为什么不能将这些模板化的函数不接受参数? [英] Why Can't These Templatized Functions Take No Arguments?

查看:135
本文介绍了为什么不能将这些模板化的函数不接受参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一对夫妇模板化的​​功能替换失败不是错​​误(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.

这篇关于为什么不能将这些模板化的函数不接受参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆