Can template arguments have nested template classes?

A

Alex

Is there something wrong with requiring that a template argument has a nested class template? Why can I not do the following?

template<typename A, typename T> struct B
{
typedef typename A::AA<T> my_type;
};

VS2013 gives a compile error, even if B is not used at all.

What could I do instead?

Thanks, Alex



p.s. what I really want to do is the following:

template<typename A>
struct B
{
template<typename T>
struct BB : A::AA<T>
{};
};
 
M

Marcel Müller

Is there something wrong with requiring that a template argument has a nested class template? Why can I not do the following?

Maybe the same problem than with forward declarations of nested classes...
template<typename A, typename T> struct B
{
typedef typename A::AA<T> my_type;
};

VS2013 gives a compile error, even if B is not used at all.

Works as expected here, even back to gcc 3.2.2.

What could I do instead?

Pass the nested type as template argument to B?


Marcel
 
V

Victor Bazarov

Is there something wrong with requiring that a template argument has
anested class template? Why can I not do the following?

template<typename A, typename T> struct B
{
typedef typename A::AA<T> my_type;

Perhaps it ought to be

typedef typename A::template AA<T> my_type;

? A wild guess...
};

VS2013 gives a compile error, even if B is not used at all.

WHAT compiler error do you get?
What could I do instead?

Thanks, Alex



p.s. what I really want to do is the following:

template<typename A>
struct B
{
template<typename T>
struct BB : A::AA<T>
{};
};

Is there a guarantee that a particular argument you provide to
instantiate 'B' actually does have a nested template? And how is
compiler to know that A::AA is a template (after which the compiler
should expect a template argument clause with angle brackets and not an
expression that can contain a less-than operator)?

V
 
A

Alex

Perhaps it ought to be



typedef typename A::template AA<T> my_type;



? A wild guess...

Perfect! Solves my problem! Thanks.
WHAT compiler error do you get?

error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'


Is there a guarantee that a particular argument you provide to

instantiate 'B' actually does have a nested template? And how is

compiler to know that A::AA is a template (after which the compiler

should expect a template argument clause with angle brackets and not an

expression that can contain a less-than operator)?

I thought using the typename keyword made that clear. And it did work on VS2012. Anyway, your solution is very logical and works.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top