decltype和成员函数(不是指针)类型 [英] decltype and member function (not pointer) type

查看:670
本文介绍了decltype和成员函数(不是指针)类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct C
{
    int Foo(int i) { return i; }

    typedef decltype(C::Foo) type;
};

因为没有类型作为成员函数类型(没有,是吗? ,我期望 C :: type int(int)

Since there is no such type as a member function type (there isn't, is there?), I expect C::type to be int (int).

但是下面的代码不能使用Visual C ++ 2012 RC编译:

But the following won't compile using the Visual C++ 2012 RC:

std::function<C::type> f;

那么什么类型是 decltype(C :: Foo)

推荐答案

代码是不成形的:成员函数名只有几种方法code> C :: Foo ),这不是其中之一(有效使用的完整列表可以在C ++语言标准中找到,参见C ++ 11§

The code is ill-formed: there are only a few ways that a member function name (e.g. C::Foo) can be used, and this is not one of them (the complete list of valid uses can be found in the C++ language standard, see C++11 §5.1.1/12).

在你的示例的上下文中,你唯一可以做的是获取成员函数的地址,& C :: Foo ,以形成 int(C :: *)(int)类型的成员函数指针。

In the context of your example, the only thing you can really do is take the address of the member function, &C::Foo, to form a pointer to the member function, of type int (C::*)(int).

由于代码格式不正确,编译器应拒绝它。此外,根据如何使用 C :: Foo ,它产生不一致的结果;

Since the code is ill-formed, the compiler should reject it. Further, it yields inconsistent results depending on how C::Foo is used; we'll look at the inconsistency below.

请报告 Microsoft Connect 。或者,让我知道,我很乐意报告此问题。

Please report a bug on Microsoft Connect. Alternatively, let me know and I am happy to report the issue.

如果您有类型,知道类型是什么,你可以通过使用它导致编译器发出错误的方式找出类型的名称。例如,声明类模板并从不定义它:

If you have a type but you don't know what the type is, you can find out the name of the type by using it in a way that causes the compiler to emit an error. For example, declare a class template and never define it:

template <typename T>
struct tell_me_the_type;

稍后,您可以使用感兴趣的类型实例化此模板:

Then later, you can instantiate this template with the type in which you are interested:

tell_me_the_type<decltype(C::Foo)> x;

由于尚未定义 tell_me_the_type x 的定义无效。编译器应该在其发出的错误中包含 T 类型。 Visual C ++ 2012 RC报告:

Since tell_me_the_type hasn't been defined, the definition of x is invalid. The compiler should include the type T in the error it emits. Visual C++ 2012 RC reports:

error C2079: 'x' uses undefined struct 'tell_me_the_type_name<T>'
with
[
    T=int (int)
]

编译器认为 C :: Foo 的类型为 int(int)。如果是这样,则编译器应该接受以下代码:

The compiler thinks that C::Foo is of type int (int). If that is the case, then the compiler should accept the following code:

template <typename T>
struct is_the_type_right;

template <>
struct is_the_type_right<int(int)> { };

is_the_type_right<decltype(C::Foo)> x;

编译器不接受此代码。它报告以下错误:

The compiler does not accept this code. It reports the following error:

error C2079: 'x' uses undefined struct 'is_the_type_right<T>'
with
[
    T=int (int)
]



C :: Foo 都是 int(int)类型,不是类型 int(int),违反了非违规原则。 : - )

So, C::Foo both is of type int (int) and is not of type int (int), which violates the principle of noncontradiction. :-)

这篇关于decltype和成员函数(不是指针)类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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