"template" and constant expressions

Discussion in 'C++' started by Noah Roberts, Apr 28, 2011.

  1. Noah Roberts

    Noah Roberts Guest

    template < bool b >
    struct whatnot
    {
    template < typename T>
    void call();
    };


    template < typename T >
    struct fun()
    {
    bool const what = a_metafunction<T>::value;

    whatnot<what>::call<T>();
    // or....
    // whatnot<what>::template call<T>();
    }

    MSVC accepts either. Which is correct? What has me confused is that
    "what" is not a dependent name, but on the other hand it's a constant
    expression that IS dependent. If what was a type I'd be certain the
    second expression should be used.
    --
    http://crazycpp.wordpress.com
    Noah Roberts, Apr 28, 2011
    #1
    1. Advertising

  2. Noah Roberts

    Ian Collins Guest

    On 04/29/11 10:15 AM, Noah Roberts wrote:
    > template< bool b>
    > struct whatnot
    > {
    > template< typename T>
    > void call();
    > };
    >
    >
    > template< typename T>
    > struct fun()


    This isn't valid C++, typo?

    --
    Ian Collins
    Ian Collins, Apr 28, 2011
    #2
    1. Advertising

  3. Noah Roberts

    Marc Guest

    Noah Roberts wrote:

    > template < typename T >
    > struct fun()
    > {
    > bool const what = a_metafunction<T>::value;
    >
    > whatnot<what>::call<T>();
    > // or....
    > // whatnot<what>::template call<T>();
    > }
    >
    > MSVC accepts either. Which is correct? What has me confused is that
    > "what" is not a dependent name, but on the other hand it's a constant
    > expression that IS dependent. If what was a type I'd be certain the
    > second expression should be used.


    You still want "template" there. Whether "what" is a type or a value
    doesn't change the fact that the compiler needs the extra information.
    Marc, Apr 29, 2011
    #3
  4. On 29 avr, 00:40, Ian Collins <> wrote:
    > On 04/29/11 10:15 AM, Noah Roberts wrote:
    >
    > > template<  bool b>
    > > struct whatnot
    > > {
    > >     template<  typename T>
    > >     void call();
    > > };

    >
    > > template<  typename T>
    > > struct fun()

    >
    > This isn't valid C++, typo?
    >


    Neither is:
    whatnot<what>::call<T>();

    Since whatnot<>::call<> is not a static member function.

    Concerning the original question, 'what' is value-dependent because is
    (§14.6.2.3) "a constant with integral or enumeration type and is
    initialized with an expression that is [value-dependent] a name
    declared with a dependent type". That makes whatnot<what> value-
    dependent.

    I expect that the '::template' form is the proper one. In fact, as I
    understand it, if it wasn't the proper form, the compiler should not
    allow it.
    Either way, IMO this is an extension of the compiler. At least
    regarding the current standard; IIRC C++0x no longer requires
    'typename' and may be 'template' to be used only in the proper place.

    --
    Michael
    Michael Doubez, Apr 29, 2011
    #4
  5. Noah Roberts

    Noah Roberts Guest

    On 4/28/2011 3:15 PM, Noah Roberts wrote:
    > template < bool b >
    > struct whatnot
    > {
    > template < typename T>
    > void call();
    > };
    >
    >
    > template < typename T >
    > void fun()
    > {
    > bool const what = a_metafunction<T>::value;
    >
    > whatnot<what>::call<what>();
    > // or....
    > // whatnot<what>::template call<what>();
    > }


    Comeau only eats the latter so I'll assume that what is a dependent name
    as well.

    --
    http://crazycpp.wordpress.com
    Noah Roberts, Apr 29, 2011
    #5
  6. Noah Roberts

    Paul Guest

    "Noah Roberts" <> wrote in message
    news:4dbae414$0$2685$...
    > On 4/28/2011 3:15 PM, Noah Roberts wrote:
    >> template < bool b >
    >> struct whatnot
    >> {
    >> template < typename T>
    >> void call();
    >> };
    >>
    >>
    >> template < typename T >
    >> void fun()
    >> {
    >> bool const what = a_metafunction<T>::value;
    >>
    >> whatnot<what>::call<what>();
    >> // or....
    >> // whatnot<what>::template call<what>();
    >> }

    >
    > Comeau only eats the latter so I'll assume that what is a dependent name
    > as well.
    >
    > --

    You can't call a nonstatic member function without an object.
    Paul, Apr 29, 2011
    #6
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,081
  2. Martin Magnusson
    Replies:
    2
    Views:
    502
    John Harrison
    Oct 8, 2004
  3. Replies:
    4
    Views:
    336
    Keith Thompson
    Dec 14, 2006
  4. Inconnu
    Replies:
    1
    Views:
    424
    Noah Roberts
    Nov 16, 2011
  5. G G
    Replies:
    3
    Views:
    89
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page