"typedef templates" inside templates - how can I do this?

Discussion in 'C++' started by Ben Pope, Dec 6, 2005.

  1. Ben Pope

    Ben Pope Guest

    Sacha wrote:
    > I'm aware, that up to date, "typedef templates" are not defined within
    > the C++ standard. The seemingly common workaround is this:
    >
    >
    > template <class T> struct MyTypeDef
    > {
    > /* ultimately I need something like this:
    > MyOtherClass<T> Type;
    > but let's keep it simple for the moment */
    >
    > typedef short Type;


    // Should that be
    typedef T Type;

    > };
    >
    > // I now can use this pseudo typedef template like this:
    >
    > MyTypeDef<int>::Type foo;
    >
    >
    > // but this does not compile (MSVC):
    >
    > template <class T> class MyClass {
    > MyTypeDef<T>::Type foo;


    // Have to let the compiler know about the dependant type.
    typename MyTypeDef<T>::Type foo;

    > };
    >
    >
    > Is there a typo in my code, or is there some fundamental limitation that
    > prohibits the typedef template workaround in this situation?


    Does that sort you out?

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Dec 6, 2005
    #1
    1. Advertising

  2. Ben Pope

    Sacha Guest

    I'm aware, that up to date, "typedef templates" are not defined within
    the C++ standard. The seemingly common workaround is this:


    template <class T> struct MyTypeDef
    {
    /* ultimately I need something like this:
    MyOtherClass<T> Type;
    but let's keep it simple for the moment */

    typedef short Type;
    };

    // I now can use this pseudo typedef template like this:

    MyTypeDef<int>::Type foo;


    // but this does not compile (MSVC):

    template <class T> class MyClass {
    MyTypeDef<T>::Type foo;
    };


    Is there a typo in my code, or is there some fundamental limitation that
    prohibits the typedef template workaround in this situation?

    -Sacha
     
    Sacha, Dec 6, 2005
    #2
    1. Advertising

  3. Ben Pope

    John Carson Guest

    "Sacha" <> wrote in message
    news:
    > I'm aware, that up to date, "typedef templates" are not defined within
    > the C++ standard. The seemingly common workaround is this:
    >
    >
    > template <class T> struct MyTypeDef
    > {
    > /* ultimately I need something like this:
    > MyOtherClass<T> Type;
    > but let's keep it simple for the moment */
    >
    > typedef short Type;
    > };
    >
    > // I now can use this pseudo typedef template like this:
    >
    > MyTypeDef<int>::Type foo;
    >
    >
    > // but this does not compile (MSVC):
    >
    > template <class T> class MyClass {
    > MyTypeDef<T>::Type foo;
    > };


    Make that

    template <class T> class MyClass {
    typename MyTypeDef<T>::Type foo;
    };


    --
    John Carson
     
    John Carson, Dec 6, 2005
    #3
  4. Ben Pope

    Guest

    Sacha wrote:
    > I'm aware, that up to date, "typedef templates" are not defined within
    > the C++ standard. The seemingly common workaround is this:
    >
    >
    > template <class T> struct MyTypeDef
    > {
    > /* ultimately I need something like this:
    > MyOtherClass<T> Type;
    > but let's keep it simple for the moment */
    >
    > typedef short Type;
    > };
    >
    > // I now can use this pseudo typedef template like this:
    >
    > MyTypeDef<int>::Type foo;
    >
    >
    > // but this does not compile (MSVC):
    >
    > template <class T> class MyClass {
    > MyTypeDef<T>::Type foo;


    typename MyTypeDef<T>::Type foo;

    > };


    Gavin Deane
     
    , Dec 6, 2005
    #4
  5. Sacha wrote:
    >
    > template <class T> class MyClass {
    > MyTypeDef<T>::Type foo;


    typename MyTypeDef<T>::Type foo;

    This will work.

    > Is there a typo in my code, or is there some fundamental limitation that
    > prohibits the typedef template workaround in this situation?


    Not a typo really. But typenames inside template qualified with the
    template parameter must be accompanied by the keyword "typename", else
    they will be treated as static variables.

    >
    > -Sacha
     
    Neelesh Bodas, Dec 6, 2005
    #5
    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:
    477
  2. Replies:
    3
    Views:
    593
  3. jimmy
    Replies:
    5
    Views:
    474
    Gianni Mariani
    Nov 29, 2005
  4. recover
    Replies:
    2
    Views:
    809
    recover
    Jul 25, 2006
  5. Replies:
    3
    Views:
    336
Loading...

Share This Page