Qubeuc
Qubeuc

Reputation: 982

Taking type of a template class

Is there a way for taking type of a template class, for example

//i have template function
template<typename T>
IData* createData();

//a template class instance
std::vector<int> a;

//using type of this instance in another template
//part in quotation mark is imaginary of course :D
IData* newData = createData<"typeOf(a)">();

is it possible in c++? or is there an shortcut alternative

Upvotes: 1

Views: 1274

Answers (2)

anon
anon

Reputation:

Not clear what you are asking about. The templates parameter is its type, for example:

template<typename T> IData* createData() {
   return new T();
}

Now we can say:

IData * id = createData <Foo>();

which will create a new Foo instance, which had better be derived from Idata.

Upvotes: 2

Dario
Dario

Reputation: 49208

Yes - Use boost::typeof

IData* newData = createData<typeof(a)>();

The new standard (C++0x) will provide a builtin way for this.

Note that you could give createData a dummy-argument which the compiler could use to infer the type.

template<typename T>
IData* createData(const T& dummy);

IData* newData = createData(a);

Upvotes: 5

Related Questions