• Advertisement
Sign in to follow this  

typedef template parameters

This topic is 1571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by Cosmic314

Share this post


Link to post
Share on other sites
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);

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

 


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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement