如何在c++中检查变量的类型
How to check the type of a variable in c++
我需要一个函数来判断变量是否为Integral。返回布尔的函数
您可以将std::is_integral_v(或std::is _integral::value pre C++17(封装在两个辅助函数中,即:
template<typename T>
bool isIntegral(const T& value)
{
return std::is_integral_v<T>;
}
template<typename T>
bool isIntegral()
{
return std::is_integral_v<T>;
}
示例用法:
int i = 0;
std::cout << std::boolalpha;
std::cout << isIntegral(i) << std::endl;
std::cout << isIntegral<int>();
使用typeid在运行时查找类型。
int i = 3;
cout << "i's typeid().name: ";
cout << typeid(i).name() << endl;
相关文章:
- c++类声明时,相同的例程,不同的成员变量类型
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 创建类似于布尔值的变量类型
- std::cin 从控制台获取两种不同的变量类型,'storing'以后使用第二种类型?
- 如何使用变量模板比较 C++ 17 中的变量类型?
- 是否有一个库可以检查C++中的变量类型?
- 与不同变量类型相比,用于变量的 Clang AST 匹配器
- 继承类中的 C++ 成员变量类型重写
- 如何在编译器时动态获取元组内的变量类型C++?
- 在C++中动态分配变量类型
- 用于浮点值的最佳变量类型
- 定义变量类型
- 错误 - 变量类型 "X" 是一个抽象类 - c++
- 在函数 (c++) 中分配变量类型和大小数组
- 具有容器变量类型的类模板
- 获取变量类型名称
- 沿枚举 c++ 返回变量类型
- 是否可以通过模板指定抽象类变量类型
- 什么是变量类型位(32)
- C++ 在知道变量类型之前使用自动定义的变量