Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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/2/ionic-framework/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
C++ 有什么需要吗_C++_Decltype - Fatal编程技术网

C++ 有什么需要吗

C++ 有什么需要吗,c++,decltype,C++,Decltype,//在这种情况下,第一段代码将失败,并且 //我需要切换到第二段代码 template<typename A, typename B> // 1 auto add(A const& a, B const& b) { return a + b; } template<typename A, typename B> // 2 auto add(A const& a, B const& b) -> decltype

//在这种情况下,第一段代码将失败,并且 //我需要切换到第二段代码

 template<typename A, typename B>     // 1
 auto add(A const& a, B const& b) { return a + b; }

 template<typename A, typename B>     // 2
 auto add(A const& a, B const& b) -> decltype(a + b) { return a + b; }
template//1
自动添加(A常量和A,B常量和B){返回A+B;}
模板//2
自动添加(A常量&A,B常量&B)->decltype(A+B){返回A+B;}

第一种方法在c++14之前不起作用,引入了返回类型推断。这时您需要第二张表格。

第二张也是SFINAE-friendly@PiotrSkotnicki你介意解释一下吗?为什么第一个不是SFINAE友好的?@songyuanyao尾部返回类型中的表达式用作SFINAE表达式;如果其格式不正确,则重载将从候选集中排除。这不适用于
auto
返回类型推断(在重载解析过程中不会检查return语句中的表达式)@PiotrSkotnicki谢谢!