Jump to content

  • Log In with Google      Sign In   
  • Create Account


typedef template parameters


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Cosmic314   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 02 October 2013 - 11:34 AM

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, 02 October 2013 - 11:35 AM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14623

Like
6Likes
Like

Posted 02 October 2013 - 12:13 PM

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);


#3 Cosmic314   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 02 October 2013 - 12:32 PM

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.



#4 SiCrane   Moderators   -  Reputation: 9491

Like
2Likes
Like

Posted 02 October 2013 - 12:38 PM


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.



#5 Cosmic314   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 02 October 2013 - 12:50 PM

 


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.



#6 Cosmic314   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 02 October 2013 - 12:59 PM

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.



#7 ChaosEngine   Crossbones+   -  Reputation: 2219

Like
2Likes
Like

Posted 02 October 2013 - 02:36 PM

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

#8 Cosmic314   Members   -  Reputation: 1164

Like
0Likes
Like

Posted 02 October 2013 - 03:17 PM

That book looks good.  Thanks for the reference.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS