Dynamic Template Instantiation

Templates are expanded at compile-time, so your problem is really just the same as the following:

struct A_float {            // struct is easier when everything's public
    A(float v) : var(v) {}  // (use the ctor-initializer please!)
    ~A() {}

    float var;
};                          // don't forget the semicolon

struct A_int {
    A(int v) : var(v) {}
    ~A() {}

    int var;
};

int main() {
    WhatType* object; // What type here?
    if (/* something*/)
        object = new A_float(0.2f);
    else
        object = new A_int(3);
}

Hopefully if you saw the above code, you'd think (as well as "maybe I should be using templates") "I am going to need a common base class for this, or else I'll refactor".

When you generate the two types at compile-time using a class template, this conclusion is the same.

  • I'd recommend the refactoring, going for a solution like Puppy's; creating an inheritance hierarchy just to work around a program logic flow flaw is programming backwards!

Well, you certainly can't do that. You'll have to make A derive from another class, for example:

template<typename T>
class A : public B {
public:
    A(T v){var = v};
    ~A(){};

    T var;
}

int main(){
    B* object;
    if(/* something*/)
        object = new A<float>(0.2f);
    else{
        object = new A<int>(3);
    }
}

The easiest way to do this is to use another function.

template<typename T> void other_stuff(A<T>* object) {
    // use T here
}
int main() {
    if (condition)
        other_stuff(new A<float>(0.2f));
    else
        other_stuff(new A<int>(3));
}

This maintains all type information and does not depend on inheritance. The disadvantage of inheritance is that T cannot appear in any function interfaces, but with this situation it can.

Tags:

C++

Templates