typedef template parameters

Started by
6 comments, last by Cosmic314 10 years, 6 months ago

I create a type:


typedef std::map<std::string,std::string> StringMap;

Later I create a function template declared as:


template<typename K, typename V>
void function_name(std::map<K,V> *map_object);

I go to use the function_name as so:


function_name<StringMap>(&some_map_object);

The compiler complains with:


error:  no matching function for call to 'function_name(StringMap*)'

When I supply both types to the template arguments (std::string,std::string) in this case, everything is fine. I would have thought that the compiler might be able to infer that StringMap = std::string,std::string but it looks like I'm wrong.

Is this mainly because the compiler cannot mismatch on number of type arguments supplied in a template?

Advertisement
You have a function template with two template parameters. You then try to instantiate it using one parameter. Of course this is going to fail.

If you want the type system to deduce the K and V arguments, don't try to explicitly instantiate the function template, i.e. just do this:

function_name(&some_map_object);

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You have a function template with two template parameters. You then try to instantiate it using one parameter. Of course this is going to fail.

If you want the type system to deduce the K and V arguments, don't try to explicitly instantiate the function template, i.e. just do this:


function_name(&some_map_object);

That's what I thought. Unfortunately, 'function_name' needs to declare variables of type K and V which it then passes to an overloaded function call. Otherwise, it's good to know that I can infer types if the situation warrants it. Thanks.


Unfortunately, 'function_name' needs to declare variables of type K and V which it then passes to an overloaded function call.

How does not explicitly passing the template parameters stop you from doing that? The body of the function doesn't care how the template parameters were determined.


Unfortunately, 'function_name' needs to declare variables of type K and V which it then passes to an overloaded function call.

How does not explicitly passing the template parameters stop you from doing that? The body of the function doesn't care how the template parameters were determined.

Good point.

I'm (very slowly) learning to use templates in my programming.

Spiffy! Thanks for putting me on the right path. Now my code looks much easier on the eyes without all those '<' and '>'.

Would you guys have any recommendations for learning about advanced features of template style programming? I'm not talking about template metaprogramming. I feel like I'm missing some potential benefits of it in my code.

Would you guys have any recommendations for learning about advanced features of template style programming? I'm not talking about template metaprogramming. I feel like I'm missing some potential benefits of it in my code.


Might be slightly dated now, but the Nicolai Josuttis book contains more than you ever wanted to know about templates.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

That book looks good. Thanks for the reference.

This topic is closed to new replies.

Advertisement