0

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?

sehe
  • 374,641
  • 47
  • 450
  • 633
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
  • 1
    Boost mostly does variable number of template arguments with something like: `template ` – UncleBens Dec 04 '11 at 17:46
  • I followed your question regarding that topic. You should really get back to square one and figure out what you are actually trying to accomplish. You are way in over your head. – pmr Dec 04 '11 at 22:03
  • Even if you did a setup like UncleBens talks about your `insert` method would not compile if 2 of the types were the same. And, I agree with pmr. – David Dec 05 '11 at 00:05

1 Answers1

1

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Well.. [Here is problem I try to solve](http://stackoverflow.com/questions/8304582/how-to-store-functional-objects-with-difrent-signature-in-a-container-eg-std) here is [quite adequate answer](http://stackoverflow.com/a/8304694/1056328) and I try to automate cede generation for it so to say `my_map myMap; myMap.insert("my_method_hello", &hello_world ); int a = myMap["my_method_hello"]("Tim", 25);` - quite primitive RPC for insiode of my application use... – myWallJSON Dec 04 '11 at 17:25
  • @myWallJSON: In that question, Oli Charlesworth has left a high-rated comment which basically suggests the same thing as I do here. IMHO the answer you have accepted there is *far, far from adequate*. – Jon Dec 04 '11 at 17:27
  • I loved that answer because of API it provides - when you call function you do not need to define which tipe it belongs to... and if you do not see class insides you can be happy with simple API... – myWallJSON Dec 04 '11 at 17:30
  • @myWallJSON: You loved that answer because you did not think of the alternatives. If you did, you would have loved those more. – Jon Dec 04 '11 at 17:33
  • 2
    @myWallJSON: That answer says: "Better advice is to not do anything even remotely like whawtever it is you're trying to do." – UncleBens Dec 04 '11 at 17:48