Help needed - template type issue

Discussion in 'C++' started by JT, Oct 6, 2004.

  1. JT

    JT Guest

    I have a compiler error when using a non-dependent type declared in a
    template, if I use the type in function definitions. I think it is a
    parsing issues related to confusion with a typename. Below I have a
    simple example. Is there another solution I should be using instead of
    declaring the type outside of the template?

    Thanks,
    JT


    For a class, this works, and the typedef is available in the function
    definition...

    class Test
    {
    typedef int myIntTypedef;
    myIntTypedef funct();
    };

    Test::myIntTypedef Test::funct()
    {
    return 0;
    }

    For a template, however, the above does not work, because (I think) the
    type is not available. The compiler (VC++7.1) suggests I use typename
    for the type, which is not correct in this case.

    template <class T>
    class Test2
    {
    typedef int myIntTypedef;
    myIntTypedef funct();
    };

    template <class T>
    Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
    not a type"
    {
    return 0;
    }

    If I define the type outside of the template, it will work...

    typedef int myIntTypedef;

    template <class T>
    class Test3
    {
    myIntTypedef funct();
    };

    template <class T>
    Test3<T>::myIntTypedef Test3<T>::funct()
    {
    return 0;
    }
     
    JT, Oct 6, 2004
    #1
    1. Advertising

  2. JT wrote:
    > I have a compiler error when using a non-dependent type declared in a
    > template, if I use the type in function definitions. I think it is a
    > parsing issues related to confusion with a typename. Below I have a
    > simple example. Is there another solution I should be using instead of
    > declaring the type outside of the template?
    >
    > Thanks,
    > JT
    >
    >
    > For a class, this works, and the typedef is available in the function
    > definition...
    >
    > class Test
    > {
    > typedef int myIntTypedef;
    > myIntTypedef funct();
    > };
    >
    > Test::myIntTypedef Test::funct()
    > {
    > return 0;
    > }
    >
    > For a template, however, the above does not work, because (I think) the
    > type is not available. The compiler (VC++7.1) suggests I use typename
    > for the type, which is not correct in this case.


    How do _you_ know that it's not correct? If I define a specialisation
    of your Test2 template like this:

    template<> class Test2<char>
    {
    static std::string myIntTypedef;
    };

    then 'myIntTypedef' will NOT be a type-id for the Test2<char> but will
    still be a type-id for any other Test2 instantiation. That means that
    'myIntTypedef' _is_ type-dependent.

    >
    > template <class T>
    > class Test2
    > {
    > typedef int myIntTypedef;
    > myIntTypedef funct();
    > };
    >
    > template <class T>
    > Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
    > not a type"


    This should be

    template<class T>
    typename Test2<T>::myIntTypedef Test2<T>::funct()

    > {
    > return 0;
    > }
    >
    > If I define the type outside of the template, it will work...
    >
    > typedef int myIntTypedef;
    >
    > template <class T>
    > class Test3
    > {
    > myIntTypedef funct();
    > };
    >
    > template <class T>
    > Test3<T>::myIntTypedef Test3<T>::funct()
    > {
    > return 0;
    > }
     
    Victor Bazarov, Oct 6, 2004
    #2
    1. Advertising

  3. "JT" <> wrote in message
    news:...
    >I have a compiler error when using a non-dependent type declared in a
    > template, if I use the type in function definitions. I think it is a
    > parsing issues related to confusion with a typename. Below I have a
    > simple example. Is there another solution I should be using instead of
    > declaring the type outside of the template?
    >
    > Thanks,
    > JT
    >
    >
    > For a class, this works, and the typedef is available in the function
    > definition...
    >
    > class Test
    > {
    > typedef int myIntTypedef;
    > myIntTypedef funct();
    > };
    >
    > Test::myIntTypedef Test::funct()
    > {
    > return 0;
    > }
    >
    > For a template, however, the above does not work, because (I think) the
    > type is not available. The compiler (VC++7.1) suggests I use typename
    > for the type, which is not correct in this case.
    >


    Not sure why you think that.

    > template <class T>
    > class Test2
    > {
    > typedef int myIntTypedef;
    > myIntTypedef funct();
    > };
    >
    > template <class T>
    > Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
    > not a type"
    > {
    > return 0;
    > }
    >


    This compiles fine for me.

    template <class T>
    typename Test2<T>::myIntTypedef Test2<T>::funct()
    {
    return 0;
    }

    Compiler error message was correct as far as I can see.

    john
     
    John Harrison, Oct 6, 2004
    #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. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    568
    tom_usenet
    Jul 24, 2003
  3. IR
    Replies:
    3
    Views:
    434
  4. Replies:
    1
    Views:
    2,150
    Gianni Mariani
    Jun 8, 2007
  5. Replies:
    2
    Views:
    794
Loading...

Share This Page