mercredi 22 janvier 2020

Assign a typedef arraytype in a template

I have a templated class like this example:

template<class T>
struct Storage {
    void Push(const T& src) {
        store[i++] = src;  //Is not possible, when T is Tuppel Type
    }
    T store[10];
    int i = 0;
};

Storage<int> ok;
typedef int Tuppel[2];
Storage<Tuppel> nok;

void test() {
    int a = 42;
    Tuppel b = { 1,2 };
    ok.Push(a);
    nok.Push(b);    //Will fail
}

How can I make this template valid? Template parameter T should be one of:

  • Primitive type
  • Object Type (store[i++] = src should call the assignment operator)
  • Arraytypes like Tuppel in the example (store[i++] = src should call the assignment operator for each array element)

Aucun commentaire:

Enregistrer un commentaire