Can template arguments have nested template classes?

Discussion in 'C++' started by Alex, Apr 28, 2014.

  1. Alex

    Alex Guest

    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>
    Alex, Apr 28, 2014
    1. Advertisements

  2. Maybe the same problem than with forward declarations of nested classes...
    Works as expected here, even back to gcc 3.2.2.

    Pass the nested type as template argument to B?

    Marcel Müller, Apr 28, 2014
    1. Advertisements

  3. Perhaps it ought to be

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

    ? A wild guess...
    WHAT compiler error do you get?
    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)?

    Victor Bazarov, Apr 28, 2014
  4. Alex

    Alex Guest

    Perfect! Solves my problem! Thanks.
    error C2059: syntax error : '<'
    error C2238: unexpected token(s) preceding ';'

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

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 (here). After that, you can post your question and our members will help you out.