Search code examples
c++c++-templates

Declaring a variable type from std::type_info


Can I declare variable type just from std::type_info? I would like to do something like this:

void some_function(std::type_info type){
    type variable;
}

Is it possible to give it as the parameter to the function and initialize it in it?


Solution

  • 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.