使用字符串声明变量类型
Declare a variable type using a string
我有一个模板,我想用合适的对象类型调用它,而不必解析字符串标识符/enum/typeid.name()等。
也就是说,而不是:
switch(varType)
{
case TYPE_ONE:
templateFunction<TYPE_ONE>();
break;
case TYPE_TWO:
templateFunction<TYPE_TWO>();
break;
...
etc...
}
我更喜欢使用:
templateFunction<GetTypeFromEnum(MyEnum::INT)>();
或者更好:
templateFunction<GetTypeFromString("int")>();
有点像倒过来的打字?
事实上,我曾经在VBA中做到过这一点,所以我认为它在C++中一定是可行的。。。
你能重新定义你的templateFunction
吗?
template <typename T>
void templateFunction(const T& Dummy)
{
// Do nothing with Dummy
}
这样做,您只需要将变量varType
传递给您调用函数的位置:
templateFunction(varType);
相关文章:
- c++类声明时,相同的例程,不同的成员变量类型
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 创建类似于布尔值的变量类型
- std::cin 从控制台获取两种不同的变量类型,'storing'以后使用第二种类型?
- 如何使用变量模板比较 C++ 17 中的变量类型?
- 是否有一个库可以检查C++中的变量类型?
- 与不同变量类型相比,用于变量的 Clang AST 匹配器
- 继承类中的 C++ 成员变量类型重写
- 如何在编译器时动态获取元组内的变量类型C++?
- 在C++中动态分配变量类型
- 用于浮点值的最佳变量类型
- 定义变量类型
- 错误 - 变量类型 "X" 是一个抽象类 - c++
- 在函数 (c++) 中分配变量类型和大小数组
- 具有容器变量类型的类模板
- 获取变量类型名称
- 沿枚举 c++ 返回变量类型
- 是否可以通过模板指定抽象类变量类型
- 什么是变量类型位(32)
- C++ 在知道变量类型之前使用自动定义的变量