overloading with templates

Discussion in 'C++' started by Sensei, Aug 19, 2010.

  1. Sensei

    Sensei Guest

    Hi! I am trying to create a function template that converts a type to a
    string, unsuccessfully! What did I think of? Overloading a function,
    with a vector class, just to try if it works:

    template<class T> string toString(T src) {...};

    template<class T> string toString(vector<T> src) {...};


    What happens is that the first one is called, ignoring the other when
    passing a vector of, for example, double.

    What am I missing?

    Thanks for your help!
     
    Sensei, Aug 19, 2010
    #1
    1. Advertising

  2. On 8/19/2010 11:03 AM, Sensei wrote:
    > Hi! I am trying to create a function template that converts a type to a
    > string, unsuccessfully! What did I think of? Overloading a function,
    > with a vector class, just to try if it works:
    >
    > template<class T> string toString(T src) {...};
    >
    > template<class T> string toString(vector<T> src) {...};
    >
    >
    > What happens is that the first one is called, ignoring the other when
    > passing a vector of, for example, double.
    >
    > What am I missing?


    The fact that the first one is still a better match for the argument
    you're passing, maybe. Or the fact that the type of your argument makes
    it impossible for the compiler to create an instance of the second
    template (SFINAE)... We can't tell because you didn't post enough code.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 19, 2010
    #2
    1. Advertising

  3. Sensei <>, on 19/08/2010 15:03:46, wrote:

    > Hi! I am trying to create a function template that converts a type to a
    > string, unsuccessfully! What did I think of? Overloading a function,
    > with a vector class, just to try if it works:
    >
    > template<class T> string toString(T src) {...};
    >
    > template<class T> string toString(vector<T> src) {...};
    >
    >
    > What happens is that the first one is called, ignoring the other when
    > passing a vector of, for example, double.
    >
    > What am I missing?


    As other posters have pointed out, you should show us the exact code
    that is failing to do what you want. If I create a complete program
    following your description I get something that does _not_ show the
    problem you're lamenting:

    #include <iostream>
    #include <string>
    #include <vector>

    template<class T> std::string toString(T src) {
    return "toString(T src)";
    };

    template<class T> std::string toString(std::vector<T> src) {
    return "toString(vector<T> src)";
    };

    int main() {
    // prints "toString(std::vector<T> src)" as expected
    std::cout << toString(std::vector<double>()) << std::endl;
    }



    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Aug 19, 2010
    #3
  4. Sensei

    Ron AF Greve Guest

    Hi,

    As the others said post code:

    #include <vector>
    #include <string>
    #include <iostream>

    using namespace std;

    template<class T> string toString(T src)
    {
    return "this is called";
    };

    template<class T> string toString(vector<T> src)
    {
    return "that is called";
    }


    int main()
    {
    cout << toString( 1.3 ) << endl;
    cout << toString( vector<double>( 10 ) ) << endl;

    return 0;
    }


    ronafgreve@informationsuperhighway:~/test$ g++ t.cc
    ronafgreve@informationsuperhighway:~/test$ ./a.out
    this is called
    that is called


    no problem here.



    "Sensei" <> wrote in message
    news:...
    | Hi! I am trying to create a function template that converts a type to a
    | string, unsuccessfully! What did I think of? Overloading a function,
    | with a vector class, just to try if it works:
    |
    | template<class T> string toString(T src) {...};
    |
    | template<class T> string toString(vector<T> src) {...};
    |
    |
    | What happens is that the first one is called, ignoring the other when
    | passing a vector of, for example, double.
    |
    | What am I missing?
    |
    | Thanks for your help!

    Regards, Ron AF Greve

    http://informationsuperhighway.eu
     
    Ron AF Greve, Aug 19, 2010
    #4
  5. Sensei

    Sensei Guest

    "Ron AF Greve" <me@localhost> wrote:
    > Hi,
    >
    > As the others said post code:



    Ok, thanks to all of you, I'll post an exhaustive code then!

    After fixing my code, now I am trying to do the other way round: from a
    string to either a type or a vector. However, I cannot compile my code,
    due to a "call to fromString(std::string &) is ambiguous":

    #include <vector>
    #include <string>
    #include <iostream>

    using namespace std;

    template<class T> T fromString(string src)
    {
    return "output T" << endl;
    };

    template<class T> vector<T> fromString(string src)
    {
    return "output vector of T" << endl;
    };


    int main()
    {
    string s("3.1415");
    cout << fromString<double>( s ) << endl;

    return 0;
    }


    I hope my code is ok. I paid attention to specializing the call to the
    template function, but as I said, I am learning C++ so I am still making
    mistakes!

    If I specialize the call with an explicit reference to a double output,
    why is it still ambiguous?

    Thanks & cheers!

    --
    Sensei@iPhone
     
    Sensei, Aug 20, 2010
    #5
  6. Sensei

    Öö Tiib Guest

    On 20 aug, 11:02, Sensei <> wrote:
    > "Ron AF Greve" <me@localhost> wrote:
    >
    > > Hi,

    >
    > > As the others said post code:

    >
    > Ok, thanks to all of you, I'll post an exhaustive code then!
    >
    > After fixing my code, now I am trying to do the other way round: from a
    > string to either a type or a vector. However, I cannot compile my code,
    > due to a "call to fromString(std::string &) is ambiguous":
    >
    > #include <vector>
    > #include <string>
    > #include <iostream>
    >
    > using namespace std;
    >
    > template<class T> T fromString(string src)
    > {
    >    return "output T" << endl;
    >
    > };
    >
    > template<class T> vector<T> fromString(string src)
    > {
    >    return "output vector of T" << endl;
    >
    > };
    >
    > int main()
    > {
    >    string s("3.1415");
    >    cout << fromString<double>( s ) << endl;
    >
    >    return 0;
    >
    > }
    >
    > I hope my code is ok. I paid attention to specializing the call to the
    > template function, but as I said, I am learning C++ so I am still making
    > mistakes!
    >
    > If I specialize the call with an explicit reference to a double output,
    > why is it still ambiguous?
    >
    > Thanks & cheers!
    >
    > --
    > Sensei@iPhone


    You need to go few books back and discover C++ without templates
    first. Your posted code is buggy because of something like that:

    #include <vector>
    #include <string>
    #include <iostream>

    using namespace std;

    double fromString( string src )
    {
    return 0.1;
    };

    vector<double> fromString( string src )
    {
    return vector<double>();
    };

    int main()
    {
    string s("3.1415");
    cout << fromString( s ) << endl;

    return 0;
    }

    What is ambiguous?
     
    Öö Tiib, Aug 20, 2010
    #6
  7. * Sensei, on 20.08.2010 10:02:
    > "Ron AF Greve"<me@localhost> wrote:
    >> Hi,
    >>
    >> As the others said post code:

    >
    >
    > Ok, thanks to all of you, I'll post an exhaustive code then!
    >
    > After fixing my code, now I am trying to do the other way round: from a
    > string to either a type or a vector. However, I cannot compile my code,
    > due to a "call to fromString(std::string&) is ambiguous":
    >
    > #include<vector>
    > #include<string>
    > #include<iostream>
    >
    > using namespace std;
    >
    > template<class T> T fromString(string src)
    > {
    > return "output T"<< endl;
    > };
    >
    > template<class T> vector<T> fromString(string src)
    > {
    > return "output vector of T"<< endl;
    > };
    >
    >
    > int main()
    > {
    > string s("3.1415");
    > cout<< fromString<double>( s )<< endl;
    >
    > return 0;
    > }
    >
    >
    > I hope my code is ok. I paid attention to specializing the call to the
    > template function, but as I said, I am learning C++ so I am still making
    > mistakes!


    Oh well. A C++ function has a /type/ consisting of its return type and the types
    of the arguments, and wrt. type your two 'fromString' are different. However,
    the return type does not matter with respect to overload resolution, that is,
    with respect to choosing which function to call: the return type is not
    considered, you can't overload on return type. The type minus the return type is
    called the function /signature/, and that's all that matters.

    Wrt. signature both your 'fromString' are identical.

    One simple fix is to call the second function e.g. 'fromStringVector', invoked
    like 'fromStringVector<double>( "blah" )'.

    With that you'll get rid of the ambiguity, but you'll discover that your return
    statements are not correct.

    You can then use e.g. std::istringstream (inside the functions) to convert.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Aug 20, 2010
    #7
    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:
    479
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,413
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    451
    Fredrik Lundh
    Sep 30, 2005
  4. Steve Holden
    Replies:
    0
    Views:
    431
    Steve Holden
    Sep 30, 2005
  5. recover
    Replies:
    2
    Views:
    812
    recover
    Jul 25, 2006
Loading...

Share This Page