如何获取模板的类型,如模板<类型名 T>,并检查 T 是 int 还是浮点数或类
How to get the type of a template, like template <typename T>, and to check if T is int or float or a class
问题
简要描述如下:
template <typename T>
void display(T data){
if(is_int(T)) // how to check if T is int in this function is_int
printf("<int> %d", data);
if(is_float(T)) // how to check if T is float in this function is_float
printf("<int> %f", data);
if(is_class(T)) // how to check if T is class in this function is_class
data.display();
}
这里假设 T 可以是 int 的类型、float 或类的类型。
如果我定义了一些变量并想使用相同的函数显示它们的值:
int a = 10:
float b = 2.7;
A_Class c;
display(a);
display(b);
display(c);
display(new int(3));
display(new float(1.899));
display(new float(1));
我知道在C++中,有一种检查int和float的解决方案(仅用于打印问题),即使用std::cout,如此问题中所述(C++模板 - 如何查找模板类型是基本类型还是类)。
使用 std::is_integral::value 不适用于以下情况:
display(new int(3));
display(new float(1.899));
display(new float(1));
因为这些变量是类而不是基本类型。那么对于这种情况,我们如何判断新int(),new float()的类型(int或float)?
要打印int
和float
值,只需分别提供采用这些类型参数的display()
重载。对于包含名为 display()
的成员函数的对象,可以使用 SFINAE 有选择地启用 display()
的自由函数形式
#include <iostream>
#include <type_traits>
template<typename T>
auto display(T const& t) -> decltype(t.display(), void())
{
std::cout << "display(T const& t)n";
}
void display(int)
{
std::cout << "display(int)n";
}
void display(double)
{
std::cout << "display(double)n";
}
struct foo
{
void display() const
{
std::cout << "foo::displayn";
}
};
struct bar {};
int main()
{
display(10);
display(10.0);
display(foo());
// display(bar()); // doesn't compile
}
现场演示当您打电话给display(bar());
时会发生什么
main.cpp:35:18: error: no matching function for call to 'display(bar)'
display(bar()); // doesn't compile
...
main.cpp:5:49: error: 'const struct bar' has no member named 'display'
您直接提供版本,检查由<type_traits>
提供:
template <typename T>
typename std::enable_if<std::is_same<T, int>::value>::type
display(T data){
printf("<int> %d", data);
}
template <typename T>
typename std::enable_if<std::is_same<T, float>::value>::type
display(T data){
printf("<int> %f", data);
}
template <typename T>
typename std::enable_if<std::is_class<T>::value>::type
display(const T& data){ // you probably don't want to copy the argument
data.display();
}
实现的一种方法是使用数字限制。但是,这是为了检查它是整数还是浮点数。
您可以执行以下操作:
#include<limits>
template <typename T>
void display(T data){
if(std::numeric_limits<T>::is_signed) // how to check if T is int in this function is_int
printf("<int> %d", data);
else // how to check if T is float in this function is_float
printf("<int> %f", data);
}
相关文章:
- C++ 模板类型检查标准::is_same不起作用?
- C++将一个指针分配给另一个指针时执行的类型检查
- 为什么 std::bind 静态类型检查传递给函数的参数?
- 避免使用 std::any 编写相同的重复类型检查代码
- 标准::enable_if类型检查
- 有没有办法在 C++17 中创建编译时类型映射以进行类型检查?
- 如果方法不进行类型检查,为什么C++模板匹配?
- 输入数据类型检查循环未按预期工作 (C++)
- 在编译时使用可变参数模板在 c++ 中使用参数类型检查获取函数的参数数
- 标准::键映射和/或 boost::any 类型的值的类型检查
- 类型检查的编译时失败
- 带有类型检查的可变参数模板
- 通过SWIG禁用python-c 接口的隐式类型检查代码
- C++SFINAE运算符/函数结果类型检查
- C 功能返回模板类型检查
- Dynamic_cast类型检查功能没有铸件
- C++模板元编程静态类型检查
- 仅当我提供类型检查规则时才"No type checking rule"错误
- Do模板提供类型检查
- 类型检查:查看变量是继承的还是某个类