什么是具有两个参数的decltype? [英] What is decltype with two arguments?

查看:1067
本文介绍了什么是具有两个参数的decltype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


编辑,以避免混淆: decltype 接受两个参数。请参阅解答。

Edit, in order to avoid confusion: decltype does not accept two arguments. See answers.

以下两个结构可用于检查类型 T 在编译期间:

The following two structs can be used to check for the existance of a member function on a type T during compile-time:

// Non-templated helper struct:
struct _test_has_foo {
    template<class T>
    static auto test(T* p) -> decltype(p->foo(), std::true_type());

    template<class>
    static auto test(...) -> std::false_type;
};

// Templated actual struct:
template<class T>
struct has_foo : decltype(_test_has_foo::test<T>(0))
{};

我想的想法是在检查成员函数的存在时使用SFINAE, p-> foo()无效,只有 test 的省略号版本, c $ c> std :: false_type 。否则,为 T * 定义第一个方法,并返回 std :: true_type 。实际的switch发生在第二个类中,它继承了 test 返回的类型。与 is_same 和类似的东西相比,这看起来聪明和轻量级。

I think the idea is to use SFINAE when checking for the existance of a member function, so in case p->foo() isn't valid, only the ellipses version of test, which returns the std::false_type is defined. Otherwise the first method is defined for T* and will return std::true_type. The actual "switch" happens in the second class, which inherits from the type returned by test. This seems clever and "lightweight" compared to different approaches with is_same and stuff like that.

$ c> decltype 有两个参数,我首先看起来很奇怪,因为我认为它只是获得一个表达式的类型。当我看到上面的代码,我认为它的东西像尝试编译表达式并返回第二个类型,如果表达式无法编译失败(所以隐藏这个专业化; SFINAE)。

The decltype with two arguments first looked surprising to me, as I thought it just gets the type of an expression. When I saw the code above, I thought it's something like "try to compile the expressions and always return the type of the second. Fail if the expressions fail to compile" (so hide this specialization; SFINAE).

但是:

然后我想我可以使用这个方法来写任何有效表达式检查器,只要它取决于某些类型 T 。示例:

Then I thought I could use this method to write any "is valid expression" checker, as long as it depends on some type T. Example:

...
    template<class T>
    static auto test(T* p) -> decltype(bar(*p), std::true_type());
...

http://ideone.com/dJkLPF

这样,我想,会返回 std :: true_type 当且仅当 bar 被定义为接受 T 作为第一个参数code> T 是可转换的等等),即:如果 bar(* p)某些上下文 p 定义类型 T *

This, so I thought, will return a std::true_type if and only if bar is defined accepting a T as the first parameter (or if T is convertible, etc...), i.e.: if bar(*p) would compile if it was written in some context where p is defined of type T*.

但是,上面的修改总是到 std :: false_type 为什么是这样?我不想用一些复杂的不同代码修复它。我只是想知道为什么它不工作,因为我的预期。显然, decltype 有两个参数的工作方式与我以前的不同。我找不到任何文件;

However, the modification above evaluates always to std::false_type. Why is this? I don't want to fix it with some complicated different code. I just want to know why it doesn't work as I expected it to. Clearly, decltype with two arguments works different than I thought. I couldn't find any documentation; it's only explained with one expression everywhere.

推荐答案

这是一个以逗号分隔的表达式列表,类型与类型列表中的最后一个表达式。它通常用于验证第一个表达式是否有效(可编译,认为SFINAE),第二个用于指定 decltype 应返回大小写第一个表达式有效。

It's an comma-separated list of expressions, the type is identical to the type of the last expression in the list. It's usually used to verify that the first expression is valid (compilable, think SFINAE), the second is used to specify that decltype should return in case the first expression is valid.

这篇关于什么是具有两个参数的decltype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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