Thread: Why can't it recognize template method?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Why can't it recognize template method?

    When I try this code, it says "no matching function for..."

    Code:
    template <typename MyType>
    std::vector<MYType> get( ...elided... )
    {
         ...elided...
    }
    
    int main()
    {
        std::vector<std::vector<std::string> ret = get( ...elided... );
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Presumably the problem is in the "...elided..." bits. The compiler can only "guess" a type from a template that it gets for a parameter.

    Soma

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by phantomotap View Post
    Presumably the problem is in the "...elided..." bits. The compiler can only "guess" a type from a template that it gets for a parameter.

    Soma
    No, I believe it's because the only possible differentiation in the instances (specializations) of the function is in the return type. So I think (and I could be wrong) that overloaded methods have to differ by args not just return type. Is that correct?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The compiler can only "guess" a type from a template that it gets for a parameter.
    O_o

    Well, obviously that's not true. I don't even know why I said it.

    For a free function used as a parameter to a constructor, this context, it is true.

    Soma
    Last edited by phantomotap; 04-11-2008 at 08:58 PM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by phantomotap View Post
    O_o

    Well, obviously that's new true. I don't even know why I said it.

    For a free function used as a parameter to a constructor, this context, it is true.

    Soma
    Whoops. Sorry, I misread your comment.

    Thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 6tr6tr View Post
    No, I believe it's because the only possible differentiation in the instances (specializations) of the function is in the return type. So I think (and I could be wrong) that overloaded methods have to differ by args not just return type. Is that correct?
    Yes, this is true. And overloaded function cannot merely differ by return type. The arguments must differ, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As Elysia said:
    ...elided... as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    Yes, this is true. And overloaded function cannot merely differ by return type. The arguments must differ, as well.
    While this is correct, it is irrelevant to the problem. Template instantiations do not overload each other. fn<Foo>() and fn<Bar>() are completely different functions. This is how, for example, lexical_cast, any_cast, and the many variants of get() work in Boost:
    Code:
    template <typename T>
    T any_cast(const any &a);
    
    // Usage:
    any a(100);
    int i = any_cast<int>(a);
    
    template <int N, typename T1, typename T2, COMPLICATED_PP_METAPROGRAM>
    complicated_metaprogram get(const tuple<T1, T2, COMPLICATED_PP_METAPROGRAM> &t);
    
    // Usage:
    tuple<int, float, Foo> tpl;
    int i = get<0>(tpl);
    float f = get<1>(tpl);
    Foo foo = get<2>(tpl);
    Note that in both cases, the arguments to the variants are always the same. Template arguments are sufficient to distinguish functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by CornedBee View Post
    While this is correct, it is irrelevant to the problem. Template instantiations do not overload each other. fn<Foo>() and fn<Bar>() are completely different functions. This is how, for example, lexical_cast, any_cast, and the many variants of get() work in Boost:
    Code:
    template <typename T>
    T any_cast(const any &a);
    
    // Usage:
    any a(100);
    int i = any_cast<int>(a);
    
    template <int N, typename T1, typename T2, COMPLICATED_PP_METAPROGRAM>
    complicated_metaprogram get(const tuple<T1, T2, COMPLICATED_PP_METAPROGRAM> &t);
    
    // Usage:
    tuple<int, float, Foo> tpl;
    int i = get<0>(tpl);
    float f = get<1>(tpl);
    Foo foo = get<2>(tpl);
    Note that in both cases, the arguments to the variants are always the same. Template arguments are sufficient to distinguish functions.
    You're right!

    The problem was in what I was specializing to. I'd
    accidentally done:

    Code:
         vector<vector<string> > ret = get<vector<string> >( c );
    when it should have been:

    Code:
         vector<vector<string> > ret = get<string>( c );
    Thanks!

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    *grumble*

    My second edit was not accepted...

    Anyway, In rare cases the compiler will choose a function based only on the return type. With the right bit of machinery you can force the compiler to examine the target result. If it wasn't for the silly rules regarding when precisely a type is known, even this would not be necessary--explicitly stating the template arguments.

    (I thought the OP was asking about how to do that in the case of a constructor.)

    Also, are you, the OP, going to at least tell the DEVX forum that you found a solution?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM