Default Parameters in Nested Templates ?

Discussion in 'C++' started by Michael Stembera, Nov 19, 2003.

  1. I would like to use default parameters in nested templates but MS VC++
    7.1 chokes on it. Does anyone know how to fix the simple example
    below or if indeed it is possible?


    template <int N=7>
    class A
    {
    };

    template < template <int N=7> class T >
    class B
    {
    A<> a_default; // A works fine using 7 as the default value

    T<5> t_5; // T works fine using 5 explicitly

    T<> t_default; // error C2976: 'T' : too few template arguments
    // why is the default value 7 not getting used here ?
    };

    int main(int, char**)
    {
    B< A > b;

    return 0;
    }

    Thanks in advance!
    Michael

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Michael Stembera, Nov 19, 2003
    #1
    1. Advertising

  2. "Michael Stembera" <> wrote...
    > I would like to use default parameters in nested templates but MS VC++
    > 7.1 chokes on it. Does anyone know how to fix the simple example
    > below or if indeed it is possible?
    >
    >
    > template <int N=7>
    > class A
    > {
    > };
    >
    > template < template <int N=7> class T >


    Did you actually mean to say

    template<class T = A<7> >

    ???

    > class B
    > {
    > A<> a_default; // A works fine using 7 as the default value
    >
    > T<5> t_5; // T works fine using 5 explicitly
    >
    > T<> t_default; // error C2976: 'T' : too few template arguments
    > // why is the default value 7 not getting used here ?


    I don't think the syntax you tried is actually allowed...

    > };
    >
    > int main(int, char**)
    > {
    > B< A > b;
    >
    > return 0;
    > }


    Victor
     
    Victor Bazarov, Nov 20, 2003
    #2
    1. Advertising

  3. Michael Stembera wrote:

    > I would like to use default parameters in nested templates but MS VC++
    > 7.1 chokes on it. Does anyone know how to fix the simple example
    > below or if indeed it is possible?
    >
    > template <int N=7>
    > class A {};
    >
    > template < template <int N=7> class T >
    > class B
    > {
    > A<> a_default; // A works fine using 7 as the default value
    >
    > T<5> t_5; // T works fine using 5 explicitly
    >
    > T<> t_default; // error C2976: 'T' : too few template arguments
    > // why is the default value 7 not getting used here ?



    From the Standard itself, it's not clear whether or not his
    should work. There is a defect report (#150, part 2) on
    this. The "rationale" section of the DR says

    | Default arguments are allowed for the parameters of a
    | template template parameter, and those default arguments
    | alone will be considered in a specialisation of the
    | template template parameter within a template defintion;
    | any default arguments for the parameters of teh template
    | template argument are ignored.

    This means that your example should compile, and should use
    the default argument, 7. To clarify which 7 this is picking
    up, let me slightly modify your example:

    template <int N = 7> class A {};
    template < template <int N = 5> class T > class B {
    A<> a; // uses A<7>
    T<> t; // uses A<5>
    };

    --
    Richard Smith

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Richard Smith, Nov 21, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    480
  2. BRG
    Replies:
    11
    Views:
    6,395
  3. Replies:
    2
    Views:
    674
    mlimber
    Jun 9, 2006
  4. recover
    Replies:
    2
    Views:
    814
    recover
    Jul 25, 2006
  5. William Xu
    Replies:
    8
    Views:
    656
    Triple-DES
    Jun 3, 2008
Loading...

Share This Page