Class nested inside a template class as template function argument type

Discussion in 'C++' started by claudiu, Apr 1, 2011.

  1. claudiu

    claudiu Guest

    Hi,

    I run into a bit of trouble trying to compile the code below.

    template<typename T>
    struct Out
    {
    struct In{};

    };

    template<typename T>
    void use(const typename Out<T>::In& r){}

    void test()
    {
    use(Out<int>::In());

    }

    Using Comeau compiler I get the error:

    line 13: error: no instance of function template "use" matches the
    argument list
    The argument types that you used are: (Out<int>::In)
    use(Out<int>::In());
    ^

    It looks like the template function use cannot deduce the argument
    type but I can't understand why. Maybe someone out there can shed some
    light on this?

    Many thanks,
    Claudiu
     
    claudiu, Apr 1, 2011
    #1
    1. Advertising

  2. Re: Class nested inside a template class as template functionargument type

    On 1 avr, 14:12, claudiu <> wrote:
    > Hi,
    >
    > I run into a bit of trouble trying to compile the code below.
    >
    > template<typename T>
    > struct Out
    > {
    >   struct In{};
    >
    > };
    >
    > template<typename T>
    > void use(const typename Out<T>::In& r){}
    >
    > void test()
    > {
    >   use(Out<int>::In());
    >
    > }
    >
    > Using Comeau compiler I get the error:
    >
    > line 13: error: no instance of function template "use" matches the
    >           argument list
    >             The argument types that you used are: (Out<int>::In)
    >     use(Out<int>::In());
    >     ^
    >
    > It looks like the template function use cannot deduce the argument
    > type but I can't understand why. Maybe someone out there can shed some
    > light on this?


    IIRC it is a non deduceable case.

    The standard lists the ways a function template parameter may be
    deduced and a dependant name is not part of it.

    But it may be deduced from other parameters.

    --
    Michael
     
    Michael Doubez, Apr 1, 2011
    #2
    1. Advertising

  3. claudiu

    Marc Guest

    claudiu wrote:

    > I run into a bit of trouble trying to compile the code below.
    >
    > template<typename T>
    > struct Out
    > {
    > struct In{};
    >
    > };


    People often hit this with std::vector<T>::iterator

    > template<typename T>
    > void use(const typename Out<T>::In& r){}
    >
    > void test()
    > {
    > use(Out<int>::In());
    >
    > }
    >
    > Using Comeau compiler I get the error:
    >
    > line 13: error: no instance of function template "use" matches the
    > argument list
    > The argument types that you used are: (Out<int>::In)
    > use(Out<int>::In());
    > ^


    You can call use<int>(...), or change the code to:

    template<typename T>
    struct Out_In { };
    template<typename T>
    struct Out
    {
    typedef Out_In<T> In;
    };
    template<typename T>
    void use(const Out_In<T>& r){}


    > It looks like the template function use cannot deduce the argument
    > type but I can't understand why. Maybe someone out there can shed some
    > light on this?


    Imagine that I add a specialization:
    template<> struct Out<double>
    {
    typedef Out<int>::In In;
    };

    Should the compiler deduce T as int or double now? The compiler would
    need to try every possible value of T to see if one has a type In that
    matches, too many bad things can happen, deduction was thus defined to
    only apply to direct types where it can be easily checked.
     
    Marc, Apr 1, 2011
    #3
  4. claudiu

    claudiu Guest

    Re: Class nested inside a template class as template functionargument type

    Thanks Michael, thanks Marc,

    I now found the standard paragraph describing this and the example
    makes it clear why this should be the case.

    Regards,
    Claudiu
     
    claudiu, Apr 1, 2011
    #4
    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. nw
    Replies:
    0
    Views:
    334
  2. Reckoner
    Replies:
    11
    Views:
    728
    Steven D'Aprano
    Jan 19, 2009
  3. claudiu
    Replies:
    2
    Views:
    310
    claudiu
    Apr 1, 2011
  4. AikidoGuy
    Replies:
    11
    Views:
    585
    Seebs
    Nov 21, 2011
  5. Ryan Dupuis
    Replies:
    8
    Views:
    1,392
    Eric Sosman
    Oct 2, 2013
Loading...

Share This Page