为什么不能将这些模板化的函数不接受参数?
我想用一对夫妇模板化的功能替换失败不是错误(SFINAE)。我可以做这样的:
I am trying to use a couple templatized functions for Substitution Fail Is Not An Error(SFINAE). And I can do it like this:
template<typename R, typename S = decltype(declval<R>().test())> static true_type Test(R*);
template<typename R> static false_type Test(...);
但我不理解的说法是如何使这个SNFIAE工作。好像我应该能够删除的参数和模板选择将工作完全相同的方式:
But I'm not understanding how the argument makes this SNFIAE work. It seems like I should just be able to remove the arguments and the template selection would work the exact same way:
template<typename R, typename S = decltype(declval<R>().test())> static true_type Test();
template<typename R> static false_type Test();
不过,这不,我得到:
But it does not, I get:
重载'测试()的呼唤是模糊的。
Call of overloaded 'Test()' is ambiguous
这是关于这些争论,使这个SFINAE工作?
What is it about these arguments that make this SFINAE work?
您第二个例子编译失败,因为有测试的两个重载
具有相同签名,becasue违约模板类型参数不是函数签名的一部分。这是不允许的。
Your second example fails to compile, since there are two overloads of Test
with identical signature, becasue defaulted template type arguments are not part of function signature. That is not allowed.
您第一个例子工程followign方式:
Your first example works in followign manner:
当键入研究
并有一个函数测试
在里面,既测试
成为有效候选人的过载。然而,省略号功能比非省略号那些排名较低,因此编译器会选择第一个过载,返回 true_type
。
When type R
does have a function test
in it, both Test
become valid overload candidates. However, ellipsis functions have lower rank than non-ellipsis ones, and thus compiler selects the first overload, returning true_type
.
当R没有测试
就可以了,先过载排除过载的分辨率设定(SFINAE在作品)。你只剩下第二个,它返回 false_type
。
When R does not have test
on it, the first overload is excluded from overload resolution set (SFINAE at works). You are left with only the second one, which returns false_type
.