Přemysl Šťastný Přemysl Šťastný - 4 months ago 17
C++ Question

Can I declare variable just from std::type_info?

Can I declare variable just from

std::type_info
? I would like to do something like this:

void some(type_info type)
{
type variable;


I would like to give as the parameter to function type of class and initialize it in it.

Answer

Your best bet is to use a template:

template <typename T>
void some()
{
    T variable;
}

//usage:
some<int>();

As C++ is statically typed, it is impossible to determine vairable type in runtime, so all types should be known at compile time anyway.

Comments