Storing a type in C++

后端 未结 8 1449
春和景丽
春和景丽 2020-12-05 17:41

Is it possible to store a type name as a C++ variable? For example, like this:

type my_type = int; // or string, or Foo, or any other type
void* data = ...;
         


        
8条回答
  •  忘掉有多难
    2020-12-05 17:45

    You can't do that in C++, but you can use the boost any library then test for the type it holds. Example:

    bool is_int(const boost::any & operand)
    {
      return operand.type() == typeid(int);
    }
    

    http://www.boost.org/doc/libs/1_42_0/doc/html/any/s02.html

提交回复
热议问题