从true值到std::true_type的隐式转换
Implicit conversion from true value to std::true_type
这是我为研究C++而编写的一个简单的模板程序:
#include <type_traits>
#include <iostream>
using namespace std;
template<typename T>
T foo(T t, true_type)
{
cout << t << " is integral! ";
return 2 * t;
}
template<typename T>
T foo(T t, false_type)
{
cout << t << " ain't integral! ";
return -1 * (int)t;
}
template<typename T>
T do_foo(T t){
return foo(t, is_integral<T>());
}
int main()
{
cout << do_foo<int>(3) << endl;
cout << do_foo<float>(2.5) << endl;
}
它不做任何花哨的事情,但它确实可以编译和工作。
我想知道零件is_integral<T>()
是如何工作的?
我在读这篇文章:http://en.cppreference.com/w/cpp/types/is_integral我找不到任何关于这种行为的具体描述-没有operator()
的定义
is_integral<T>
是继承自true_type
或false_type
的类型。
is_integral<T>()
是一个构造函数调用,因此其中一个类型的实例就是对foo
调用的参数。然后根据过载选择。
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 尝试将字符串变量转换为布尔值会导致 "true" 和 "false" 都等于 0
- 从true值到std::true_type的隐式转换
- "true"并不总是转换为 1
- 如果是字符或字符串返回false,但如果输入不能转换为整数则返回true.c++的while循环