【发布时间】:2018-01-16 12:06:04
【问题描述】:
Microsoft 编译器 (Visual Studio 2017 15.2) 拒绝以下代码:
#include <type_traits>
struct B
{
template<int n, std::enable_if_t<n == 0, int> = 0>
void f() { }
};
struct D : B
{
using B::f;
template<int n, std::enable_if_t<n == 1, int> = 0>
void f() { }
};
int main()
{
D d;
d.f<0>();
d.f<1>();
}
错误是:
error C2672: 'D::f': no matching overloaded function found
error C2783: 'void D::f(void)': could not deduce template argument for '__formal'
note: see declaration of 'D::f'
Clang 也拒绝它:
error: no matching member function for call to 'f'
d.f<0>();
~~^~~~
note: candidate template ignored: disabled by 'enable_if' [with n = 0]
using enable_if_t = typename enable_if<_Cond, _Tp>::type;
GCC 完全接受它。哪个编译器是对的?
加法:
在表单中使用 SFINAE
template<int n, typename = std::enable_if_t<n == 0>>
...
template<int n, typename = std::enable_if_t<n == 1>>
GCC 也会产生错误:
error: no matching function for call to ‘D::f<0>()’
d.f<0>();
^
note: candidate: template<int n, class> void D::f()
void f()
^
note: template argument deduction/substitution failed:
【问题讨论】:
-
有了
template<int n, typename = std::enable_if_t<n == 0>>,你声明了2个相同的函数(使用不同的默认参数)。 -
@Evgeny:模板参数似乎不算数。
-
@Evgeny 不,他们没有 - 他们都有相同的 parameter-type-list:没有参数。
-
我给 Mike 发了消息。请注意,第 15 段之前的段落存在类似问题,已作为核心问题 565 解决。
标签: c++ c++14 sfinae overload-resolution using-declaration