468,511 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

Nested template type access

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?

--
Lionel B

Jul 22 '05 #1
3 3632

"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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
Jul 22 '05 #2
John Harrison wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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.


Thanks, John.

--
Lionel B

Jul 22 '05 #3

"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by CoolPint | last post: by
5 posts views Thread by Axter | last post: by
1 post views Thread by Jacob N. Rohde | last post: by
9 posts views Thread by Adam Nielsen | last post: by
8 posts views Thread by Ralf Goertz | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.