Reputation: 9522
I have created N classes that take from one to N ints into constructors (and I hoped that would be enough) but appeared I am wrong.
Here are 2 sample classes:
template < class T0 , class T1>
class my_map {
typedef T0 type_0;
typedef T1 type_1;
std::map<std::string, type_0* > T0_var;
std::map<std::string, type_1* > T1_var;
friend class apitemp;
public:
my_map( int meaningless0 = 42 , int meaningless1 = 42 ) {}
class apitemp {
std::string n_;
my_map* p;
public: apitemp(std::string name_, my_map* parent) : n_(name_), p(parent) {}
operator type_0*() {return p->T0_var[n_] ; }
operator type_1*() {return p->T1_var[n_] ; }
};
void insert(std::string name, type_0* ptr)
{ T0_var[name] = ptr; }
void insert(std::string name, type_1* ptr)
{ T1_var[name] = ptr; }
apitemp operator[](std::string n_) {return apitemp(n_, this);}
};
template < class T0>
class my_map
{
typedef T0 type_0;
std::map<std::string, type_0* > T0_var;
friend class apitemp;
public:
my_map( int meaningless0 = 42 ) {}
class apitemp
{
std::string n_;
my_map* p;
public:
apitemp(std::string name_, my_map* parent) : n_(name_), p(parent) {}
operator type_0*() {return p->T0_var[n_] ; }
};
void insert(std::string name, type_0* ptr)
{ T0_var[name] = ptr; }
apitemp operator[](std::string n_) {return apitemp(n_, this);}
};
Order does not matter... when both classes are present I can't compile my code, when one is commented (and we use API from only one of 2 classes) everything compiles... but when I try to use both I get compiler errors... So I wonder how to make such classes overridable?
Upvotes: 0
Views: 109
Reputation: 437744
You cannot "overload" a template on the number of type parameters like you are trying to do. The correct reaction to this restriction depends on what exactly you are trying to do.
It seems that your purpose here is to make a map
that maps from a string to a variable number of values (set at compile time). But you already have such a templated container: std::map
itself!
// This maps to int
std::map<std::string, int> map_to_int;
struct foo {
std::string str;
int i;
};
// This effectively maps to 2 fields
std::map<std::string, struct foo> map_to_struct;
// etc etc
If you were allowed to write
std::map<std::string, std::string*, int*> does_not_compile;
that would not give you anything that the map_to_struct
above does not already give you.
Upvotes: 1