Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template template arguments and default parameters (http://www.velocityreviews.com/forums/t754670-template-template-arguments-and-default-parameters.html)

avasilev 10-06-2011 12:19 PM

template template arguments and default parameters
 
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 amo ding 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::map<std::basic_string<char>, std::basic_string<char> >&,
int)'

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

Victor Bazarov 10-06-2011 12:44 PM

Re: template template arguments and default parameters
 
On 10/6/2011 8:19 AM, avasilev wrote:
> 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 amo ding the following:
> template<template<key, value> class T>


First, I believe this ought to be

template<template<class,class> class T>

second, it limits the number of arguments to 2, which isn't true for
most Standard containers since they usually have a comparator and an
allocator types added (although those usually have defaults).

> 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::map<std::basic_string<char>, std::basic_string<char> >&,
> int)'
>
> 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.


AFAIK, no. Matching of types/templates does not involve default
arguments. Maybe it should, but it doesn't. You could try defining
your own 'map' template specialization that would only have two
arguments to begin with, but that would require redeclaring most of the
"original" map functionality, which is what you probably want to avoid...

IIRC the new Standard will have "template typedefs" and with those you
could define your own "map" "template" that doesn't have the extra
arguments (IOW, has only two arguments) and use that in your function
template.

* * *

Another approach altogether is to define a policy argument for your
function template. Something like

template<class ContainerPolicy, class String>
bool parse(String const& input,
typename ContainerPolicy::map& output);

Then you implement your ContainerPolicy like so

template<class String> struct ContainerPolicyMap {
typedef std::map<String,String> map;
};

template<class String> struct ContainerPolicyMultimap {
typedef std::multimap<String,String> map;
};

and use those by supplying the explicit template argument:

...
std::string somestringtoparse;
std::map<string,string> mymap;
...
parse<ContainerPolicyMap>(somestringtoparse, mymap);

(or something like that, anyway). It doesn't afford you the automatic
determination of the container type, you still have to provide the
argument, unfortunately. You can work around it by adding more "trait"
type functions. For instance, the second argument of your 'parse'
function could actually be the policy, which would have the conversion
from the container, and inside the 'parse' function you'd extract the
container out of the policy object to be filled. You know, like a
binder whose sole purpose is to provide the type...

V
--
I do not respond to top-posted replies, please don't ask

avasilev 10-06-2011 01:40 PM

Re: template template arguments and default parameters
 
On Oct 6, 3:44*pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 10/6/2011 8:19 AM, avasilev wrote:
>
> > 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 amo ding the following:
> > template<template<key, value> *class T>

>
> First, I believe this ought to be
>
> * * template<template<class,class> class T>
>
> second, it limits the number of arguments to 2, which isn't true for
> most Standard containers since they usually have a comparator and an
> allocator types added (although those usually have defaults).
>
>
>
>
>
>
>
>
>
> > 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::map<std::basic_string<char>, std::basic_string<char> *>&,
> > int)'

>
> > 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.

>
> AFAIK, no. *Matching of types/templates does not involve default
> arguments. *Maybe it should, but it doesn't. *You could try defining
> your own 'map' template specialization that would only have two
> arguments to begin with, but that would require redeclaring most of the
> "original" map functionality, which is what you probably want to avoid...
>
> IIRC the new Standard will have "template typedefs" and with those you
> could define your own "map" "template" that doesn't have the extra
> arguments (IOW, has only two arguments) and use that in your function
> template.
>
> * * *
>
> Another approach altogether is to define a policy argument for your
> function template. *Something like
>
> * * template<class ContainerPolicy, class String>
> * * * *bool parse(String const& input,
> * * * * * * * * * typename ContainerPolicy::map& output);
>
> Then you implement your ContainerPolicy like so
>
> * * template<class String> struct ContainerPolicyMap {
> * * * *typedef std::map<String,String> map;
> * * };
>
> * * template<class String> struct ContainerPolicyMultimap {
> * * * *typedef std::multimap<String,String> map;
> * * };
>
> and use those by supplying the explicit template argument:
>
> * * ...
> * * * *std::string somestringtoparse;
> * * * *std::map<string,string> mymap;
> * * ...
> * * * *parse<ContainerPolicyMap>(somestringtoparse, mymap);
>
> (or something like that, anyway). *It doesn't afford you the automatic
> determination of the container type, you still have to provide the
> argument, unfortunately. *You can work around it by adding more "trait"
> type functions. *For instance, the second argument of your 'parse'
> function could actually be the policy, which would have the conversion
> from the container, and inside the 'parse' function you'd extract the
> container out of the policy object to be filled. *You know, like a
> binder whose sole purpose is to provide the type...
>
> V
> --
> I do not respond to top-posted replies, please don't ask


Yes, what you propose is doable, but I was looking for a more
lightweight solution. I guess I could just make my function callable
like this:
parse<std::map<string,string>>(...)
but this is too verbose and not so easily readable. The policy
solution will at least help for readability.
Thanks
Alex


All times are GMT. The time now is 01:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.