Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 作为表达式的模板参数语法_C++_Templates_Object_Expression_Sfinae - Fatal编程技术网

C++ 作为表达式的模板参数语法

C++ 作为表达式的模板参数语法,c++,templates,object,expression,sfinae,C++,Templates,Object,Expression,Sfinae,有人知道下面的代码如何处理enable_if的模板参数吗 template <int n> void f(typename std::enable_if<(n < 0)>::type* = 0) { /* ... n is negative ... */ } template <int n> void f(typename std::enable_if<(n >= 0)>::type* = 0) { /* ... n is positi

有人知道下面的代码如何处理enable_if的模板参数吗

template <int n> void f(typename std::enable_if<(n < 0)>::type* = 0) {
/* ... n is negative ... */
}
template <int n> void f(typename std::enable_if<(n >= 0)>::type* = 0) {
/* ... n is positive ... */
}
template void f(typename std::enable_if::type*=0){
/*…n是负数*/
}
模板无效f(typename std::enable_if=0)>::type*=0){
/*…n是正的*/
}
具体地说,这部分
(n<0)>:type*
对我来说没有意义,因为这看起来像是编译器在隐式地将表达式
(n<0)
转换为对象

在其他几个SFINAE示例中,我也看到过这种将表达式作为对象处理的方式,但这里发生的事情让我难以理解。是在某些C++头中定义的对象还是已经在标准中?如果是这样的话,这种将表达式视为对象的技术是什么?我如何才能了解更多有关这方面的信息

非常感谢

这看起来像是编译器将表达式(n<0)隐式转换为对象

否。
n<0
是的模板参数(类型为
bool
),并且
std::enable_if::type
std::enable_if
中定义的成员
typedef
(仅在
n<0
为true时存在)

模板
结构启用_if;
如果
B
true
std::enable_如果
具有公共成员类型def type,则等于
到
T
;否则,没有成员typedef

此元功能是利用 根据类型有条件地从重载解析中删除函数 traits和提供单独的函数重载和专门化 对于不同类型的性状<代码>标准::如果可以将
用作附加 函数参数(不适用于运算符重载),作为返回 类型(不适用于构造函数和析构函数),或作为类 模板或函数模板参数

template< bool B, class T = void >
struct enable_if;