telplate template arguments and default parameters

Discussion in 'C++' started by avasilev, Oct 6, 2011.

  1. avasilev

    avasilev Guest

    HI all,
    I have implemented a function that takes a string as input, parses it,
    and outputs a map<string, string> of name=value pairs. Something like:

    bool parse(const string& input, map<string, string>& output)
    However, to make it more flexible, I want to be able to change the
    kind of map that is being used, so that it can be a hash_map, multimap
    or whatever. For this purpose I need to make the function a template
    function. I want as parameter for the template the "map" class name,
    and I will use a specialized <string,string>version of it.
    I am doing the following:

    template <template <key, value> class T>
    bool parse(const string& input, T<string,string> output)
    {
    ....

    }

    then I try to call the function like this:
    std::map<string, string>;
    string input = "name=val;name1=val1"
    parse<std::map>(input , m);

    But this fails to compile, with the error (using gcc):

    error: no matching function for call to
    'parse(std::basic_string<char>&, std::map<std::basic_string<char>,
    std::basic_string<char> >&)'

    I have tried to compile an example where I define the 'map' class, and
    it takes strictly two parameters. This compiles fine. It also compiles
    if I provide the default template parameters of the std::map class:

    template < template < class k, class v, class Compare, class
    Allocator> class T >

    void parse(const string& input, T < string , string, less<string>,
    allocator<pair<const string,string> > > output)

    My question is - is there a way to make what I want without providing
    all default template parameters, the least reason being that a
    different 'map' class may have different default template parameters.

    Thanks in advance
    Best regards
    Alex
     
    avasilev, Oct 6, 2011
    #1
    1. Advertising

  2. On 10/6/2011 8:29 AM, avasilev wrote:
    > HI all,
    > I have implemented a function that takes a string as input, parses it,
    > and outputs a map<string, string> of name=value pairs. Something like:
    > [...impatient repost snipped...]


    Please learn to have patience in a newsgroup. This is not a chat room
    where somebody is just waiting to serve you.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 6, 2011
    #2
    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. Edward Diener
    Replies:
    14
    Views:
    4,975
    Josiah Carlson
    Apr 6, 2004
  2. IR
    Replies:
    3
    Views:
    421
  3. er
    Replies:
    3
    Views:
    1,085
  4. kito
    Replies:
    2
    Views:
    417
  5. avasilev
    Replies:
    2
    Views:
    516
    avasilev
    Oct 6, 2011
Loading...

Share This Page