Saturday, 26 March 2016

c++ - Typedef in template class not working

That might be a silly question, but I'm staring at this code for a while and can't think of what's wrong. On compiling:



#include 
#include


template
struct my_string_map {
typedef std::map type;
};
template bool add_to_string_map(my_string_map::type map,
std::string str, T x) {
if (map.find(str) != map.end())
return false;
map[str] = x;

return true;
}


I get:



foo.cpp:8: error: template declaration of ‘bool add_to_string_map’
foo.cpp:8: error: expected ‘)’ before ‘map’
foo.cpp:8: error: expected primary-expression before ‘str’
foo.cpp:8: error: expected primary-expression before ‘x’



(Definition of my_string_map class was taken from another thread of this forum)



When i specialize what template type I'm using, like in



bool add_to_string_int_map(my_string_map::type map,
std::string str, int x) {
if (map.find(str) != map.end())
return false;

map[str] = x;
return true;
}


everything works fine. Why it's not working and/or how to get it working?



Thanks in advance for your help

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...