小编MiC*_*Cha的帖子

为什么 `std::enable_if` 需要默认值?

为什么我必须::type = 0在这种std::enable_if用法中使用默认值( )?

\n\n

我看到一些例子,没有它也能工作。例如https://foonathan.net/blog/2015/11/30/overload-resolution-4.html

\n\n
#include<iostream>\n#include<type_traits>\n\ntemplate <typename T,\n          typename std::enable_if<std::is_integral<T>::value, T>::type = 0>\nvoid do_stuff(T t) {\n    std::cout << "do_stuff integral\\n";\n}\n\ntemplate <typename T,\n          typename std::enable_if<std::is_class<T>::value, T>::type = 0>\nvoid do_stuff(T t) {\n    std::cout << "do_stuff class\\n";\n}\n\nint main()\n{\n    do_stuff(32);\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到错误消息:

\n\n
temp.cpp:6:6: note:   template argument deduction/substitution failed:\ntemp.cpp:18:13: note:   couldn\'t deduce template parameter \xe2\x80\x98<anonymous>\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

应推论为

\n\n
template <template T, int>\nvoid do_stuff(T t)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是有效的代码。\n我做错了什么?(海湾合作委员会版本7.4.0

\n

c++ templates sfinae c++11

5
推荐指数
1
解决办法
1167
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1

templates ×1