typename, typedef, and resolution

Discussion in 'C++' started by Gina Yarmel, Aug 13, 2003.

  1. Gina Yarmel

    Gina Yarmel Guest

    The foolowing error is mysterious to me. Isn't a typedef just a
    synonym?

    template<typename T>
    typename T::me some_func(typename T::me A) {
    return A;
    }

    template<typename T>
    typename T::me other_func(T A) {
    return A;
    }

    class some_class {
    public:
    typedef some_class me;
    };

    int main() {
    some_class some_obj;
    //ERROR:
    //some_func(some_obj);
    other_func(some_obj);
    }

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

  2. "Gina Yarmel" <> wrote...
    > The foolowing error is mysterious to me. Isn't a typedef just a
    > synonym?
    >
    > template<typename T>
    > typename T::me some_func(typename T::me A) {
    > return A;
    > }
    >
    > template<typename T>
    > typename T::me other_func(T A) {
    > return A;
    > }
    >
    > class some_class {
    > public:
    > typedef some_class me;
    > };
    >
    > int main() {
    > some_class some_obj;
    > //ERROR:
    > //some_func(some_obj);
    > other_func(some_obj);
    > }


    The compiler cannot resolve a template argument based on
    a dependent name, IIRC. It cannot figure out that T is
    'some_class' out of T::me when instantiating 'some_func'.

    Victor
    Victor Bazarov, Aug 13, 2003
    #2
    1. Advertising

  3. Gina Yarmel wrote:
    > The foolowing error is mysterious to me. Isn't a typedef just a
    > synonym?


    Yes, but that has nothing to do with your problem, I think.

    > template<typename T>
    > typename T::me some_func(typename T::me A) {
    > return A;
    > }


    It can't work because supplying the type as parameter doesn't disambiguate
    the other type it is typedef'd in. Imagine this:

    struct foo
    {};
    struct s1
    {
    typedef foo me;
    };
    struct s2
    {
    typedef foo me;
    };

    some_func( foo());

    The compiler gets a foo and can't possibly determine which of s1/s2 it
    should be using. Not sure, but maybe using
    some_func<s1>( foo());
    does the trick.

    > template<typename T>
    > typename T::me other_func(T A) {
    > return A;
    > }


    Here, the compiler gets a T and looks inside T for a type/typedef called
    'me'.



    --
    Questions ?
    see C++-FAQ Lite: http://parashift.com/c -faq-lite/ first !


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Ulrich Eckhardt, Aug 13, 2003
    #3
  4. In article <>,
    Gina Yarmel wrote:
    > The foolowing error is mysterious to me. Isn't a typedef just a
    > synonym?

    <snip>

    Yes, but function template argument deduction doesn't work where the
    function parameter type is a dependent name.

    See
    <http://groups.google.com/groups?selm=slrnbisehj.1pc.do-not-spam-benh%40tin.bwsint.com>
    for a fuller explanation.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Ben Hutchings, Aug 13, 2003
    #4
  5. (Gina Yarmel) wrote in message
    news:<>...
    > The foolowing error is mysterious to me. Isn't a typedef just a
    > synonym?
    >
    > template<typename T>
    > typename T::me some_func(typename T::me A) {
    > return A;
    > }
    >
    > template<typename T>
    > typename T::me other_func(T A) {
    > return A;
    > }
    >
    > class some_class {
    > public:
    > typedef some_class me;
    > };
    >
    > int main() {
    > some_class some_obj;
    > //ERROR:
    > //some_func(some_obj);
    > other_func(some_obj);
    > }
    >


    My VC6 fails with reasonable error: could not deduce template argument
    for 'T'.
    Typedef doesnt introduce a new type. But what you could do if you were
    a c++ compiler? You have a template function call and you need to
    identify the template argument in order to instantiate the function.
    You are said: T::me is some_class, but nobody could tell you who is
    mister T himself.

    For example, we may have:

    class some_class {
    public:
    typedef some_class me;
    };

    class other_class {
    public:
    typedef some_class me;
    };

    ....
    some_class some_obj;
    some_func(some_obj); // ???? both some_class and other_class have
    "me"
    // of "some_class" type

    For this reason you should explicitly tell the compiler which function
    instance you want to use:

    some_func<some_class> (some_obj); // OK!

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Sergei Emantayev, Aug 13, 2003
    #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. Andrew FPGA
    Replies:
    0
    Views:
    960
    Andrew FPGA
    Sep 26, 2005
  2. Denis Remezov
    Replies:
    1
    Views:
    2,121
    I wish
    Jun 25, 2004
  3. Generic Usenet Account
    Replies:
    3
    Views:
    802
    Generic Usenet Account
    Jul 14, 2005
  4. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    344
    Rolf Magnus
    Mar 14, 2006
  5. oor
    Replies:
    0
    Views:
    1,335
Loading...

Share This Page