L
Lionel B
Greetings.
In a template class for which a template parameter may be another
template class, I would like to be have access to the type of the
nested template parameter. Hopefully the following example makes this
clearer:
// example1.cpp
template <typename T> struct A
{
typedef T param_type;
};
template <typename U> struct B
{
typedef U:aram_type nested_param_type; // line 10
};
int main()
{
B< A<int> > b;
return 0;
}
My compiler (gcc) throws this out with:
test.cpp:10: error: ISO C++ forbids declaration of `param_type' with no
type
test.cpp:10: error: `:aram_type' is not a valid declarator
test.cpp:10: error: parse error before `;' token
(for some reason, gcc seems to think I am trying to declare
`param_type'). On other hand, the following code compiles happily:
// example2.cpp
template <typename T> struct A
{
const static int n = 123;
};
template <typename U> struct B
{
const static int m = U::n;
};
int main()
{
B< A<int> > b;
return 0;
}
In both cases the declaration of B references an attribute of its
template parameter U (a type in example 1, a static data member in
example 2) which is known - for a particular instantiation of an object
of type B - at compile time. It's not clear to me why the former should
be acceptable and the latter not.
Any views?
In a template class for which a template parameter may be another
template class, I would like to be have access to the type of the
nested template parameter. Hopefully the following example makes this
clearer:
// example1.cpp
template <typename T> struct A
{
typedef T param_type;
};
template <typename U> struct B
{
typedef U:aram_type nested_param_type; // line 10
};
int main()
{
B< A<int> > b;
return 0;
}
My compiler (gcc) throws this out with:
test.cpp:10: error: ISO C++ forbids declaration of `param_type' with no
type
test.cpp:10: error: `:aram_type' is not a valid declarator
test.cpp:10: error: parse error before `;' token
(for some reason, gcc seems to think I am trying to declare
`param_type'). On other hand, the following code compiles happily:
// example2.cpp
template <typename T> struct A
{
const static int n = 123;
};
template <typename U> struct B
{
const static int m = U::n;
};
int main()
{
B< A<int> > b;
return 0;
}
In both cases the declaration of B references an attribute of its
template parameter U (a type in example 1, a static data member in
example 2) which is known - for a particular instantiation of an object
of type B - at compile time. It's not clear to me why the former should
be acceptable and the latter not.
Any views?