Nested template type access

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::param_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: `::param_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?
 
J

John Harrison

Lionel B said:
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::param_type nested_param_type; // line 10

typedef typename U::param_type nested_param_type; // line 10

gcc (like any compliant compiler) is assuming that param_type is a static
data member. It is your responsibility to tell it different.

john
 
L

Lionel B

John said:
typedef typename U::param_type nested_param_type; // line 10

gcc (like any compliant compiler) is assuming that
param_type is a static data member. It is your
responsibility to tell it different.

Thanks, John.
 
A

andre dajd

Lionel B said:
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::param_type nested_param_type; // line 10
};

Should be

typedef typename U::param_type nested_param_type'

Rgds
d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top