Skip to main content
GameDev.net gamedev.net
🔒 Locked

typedef template parameters

Started by Cosmic314 Oct 2, 2013 at 5:34 PM 6 replies 1.7k views
Original Post
Cosmic314
Cosmic314

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?

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

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.

SiCrane
SiCrane




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.

Cosmic314
Cosmic314

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.

Cosmic314
Cosmic314

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.

ChaosEngine
ChaosEngine

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

That book looks good. Thanks for the reference.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.