Re: Dependent type in template base class
Stephan Tolksdorf wrote:
Hi,
I'd like to make the name of a member type in a template base class
directly available to the child class. (I don't want to qualify each
reference to the type with "typename Base<T>::" in order to avoid
extremely ugly code).
I'm currently doing this with an using declaration as in the following
code snippet:
template <typename T>
class base {
struct param_type { };
void f(param_type& p) { }
};
template <typename T>
class test : base<T> {
using typename base<T>::param_type;
test(param_type& p) { } // GCC doesn't recognize param_type
}
While Comeau and Visual Studio don't complain about this code, GCC
doesn't seem to grok it.
It's seems likely that gcc interprets the following rule differently
than the other two compilers:
"A using-declaration shall not name a template-id"
[namespace.udecl/5]
with examples of illegal using declarations:
using A::f<double>; // ill-formed
using A::X<int>; // ill-formed
But param_type is not itself a template-id, but a member of a template.
So does the using declaration:
using typename base<T>::param_type;
"name" base<T>, or does it just name param_type, or both? I will post
that question on comp.std.c++ to see whether anyone knows for certain.
Could I use a typedef instead of the using declaration as a workaround,
or would that entail other problems?
There seems to be little question that a typedef:
typedef base<T>::param_type param_type;
would be legal. So I would recommend using the typedef as the less
controversial choice. After all, there's no point in waking up one day
only to find out that gcc was right all along - and that the using
declaration was wrong after all.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]