Sign in to follow this  
Cosmic314

typedef template parameters

Recommended Posts

Cosmic314    2002

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
Cosmic314    2002

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
SiCrane    11839


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
Cosmic314    2002

 


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
Cosmic314    2002

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
ChaosEngine    5185

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this