Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare a template pointer without knowing the type?

Tags:

This is what I would like to do:

ExampleTemplate* pointer_to_template;
cin >> number;
switch (number) {
case 1:
    pointer_to_template = new ExampleTemplate<int>();
    break;
case 2:
    pointer_to_template = new ExampleTemplate<double>();
    break;
}
pointer_to_template->doStuff();

This doesn't compile because the template type must be specified when declaring the pointer. (ExampleTemplate* pointer_to_template should be ExampleTemplate<int>* pointer_to_template.) Unfortunately, I don't know the type of the template until it's declared in the switch block. What is the best work around for this situation?

like image 602
Nathan Avatar asked Nov 21 '11 00:11

Nathan


People also ask

How do you declare a template in C++?

Class Template DeclarationA class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };

How a template is declared?

A class template must be declared before any instantiation of a corresponding template class. A class template definition can only appear once in any single translation unit. A class template must be defined before any use of a template class that requires the size of the class or refers to members of the class.

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.


2 Answers

You can't. ExampleTemplate<int> and ExampleTemplate<double> are two different, unrelated types. If you always have a switch over several options, use boost::variant instead.

typedef boost::variant<Example<int>, Example<double>> ExampleVariant;
ExampleVariant v;
switch (number) {
    case 1: v = Example<int>(); break;
    case 2: v = Example<double>(); break;
}
// here you need a visitor, see Boost.Variant docs for an example

Another way is to use an ordinary base class with virtual public interface, but I'd prefer variant.

struct BaseExample {
    virtual void do_stuff() = 0;
    virtual ~BaseExample() {}
};

template <typename T>
struct Example : BaseExample { ... };

// ..
BaseExample *obj;
like image 156
Cat Plus Plus Avatar answered Oct 05 '22 15:10

Cat Plus Plus


You can do something similar by having your template class derive from a regular class:

#include<iostream>
#include<sstream>
using namespace std;

class ExampleBase{

public:
    virtual ~ExampleBase() {}
    virtual string Get() = 0;
};

template<typename T>
class ExampleTemplate : public ExampleBase{

private:
    T data;

public:
    ExampleTemplate(T t) : data(t){}

    string Get(){        
        stringstream s; s << data;
        return s.str();
    }

};

int main(){

    ExampleBase *base;
    int number;
    cout << "> " << flush; cin >> number;

    switch(number){
        case 1:
            base = new ExampleTemplate<int>(42);
            break;
        case 2:
            base = new ExampleTemplate<double>(3.14);
            break;
        default:
            return 1;
    }

    cout << base->Get() << endl;

    delete base;
    return 0;
}
like image 22
Vlad Avatar answered Oct 05 '22 13:10

Vlad