views:

131

answers:

2

Hi everyone i have a question about templates,

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

+5  A: 

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);
Dario
thanks............
Qubeuc
+2  A: 

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.

anon