This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
typedefs in class templates and their (non)inheritance
- From: =?big5?b?YWxleGh1YW5nKLbAsVLAcyk=?= <alexhuang at gorilla dot com dot tw>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 5 Aug 2009 18:55:38 +0800
- Subject: typedefs in class templates and their (non)inheritance
Are typedefs in class templates required to be redeclared in descendents?
The following code does not compile using gcc 4.2.2
-----------------------------------
template <typename T>
struct MyFunctor : public std::unary_function<int, T>
{
result_type operator()(argument_type arg)
{
return arg;
}
};
MyFunctor<int> foo;
-----------------------------------
error: ISO C++ forbids declaration of ¡¥result_type¡¦ with no type
The compiler says that result_type and argument_type are not defined. They are defined in std::unary_function, but apparently not inherited. There is a thread http://gcc.gnu.org/ml/gcc-bugs/1997-10/msg00045.html which explains that these typedefs cannot be inherited because std::unary_function may be specialized on <int, int> and the typedefs might not exist in that specialization. Can't the compiler discover whether or not the specialization exists and hence whether or not the typedefs exist? Redeclaring typedefs seems a bit error-prone to me -- plus, I'm lazy.
The following code does compile using gcc 4.2.2
-----------------------------------
template <typename T>
struct MyFunctor : public std::unary_function<int, T>
{
typedef typename std::unary_function<int, T>::result_type result_type;
typedef typename std::unary_function<int, T>::argument_type argument_type;
result_type operator()(argument_type arg)
{
return arg;
}
};
MyFunctor<int> foo;
-----------------------------------
Am I missing some other construct that will make std::unary_function's typedefs visible in MyFunctor?
Thanks,
Alex