简体   繁体   English

decltype和成员函数(不是指针)类型

[英]decltype and member function (not pointer) type

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

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

Since there is no such type as a member function type (there isn't, is there?), I expect C::type to be int (int) . 由于不存在成员函数类型(不存在,不存在?),因此我期望C::typeint (int)

But the following won't compile using the Visual C++ 2012 RC: 但是以下内容无法使用Visual C ++ 2012 RC进行编译:

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

So what type is decltype(C::Foo) ? 那么decltype(C::Foo)是什么类型?

The code is ill-formed: there are only a few ways that a member function name (eg 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 ),但这不是其中一种(可以在C ++语言中找到有效用法的完整列表)标准,请参见C ++ 11§5.1.1/ 12)。

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的地址,以形成指向成员函数的指针,该指针的类型为int (C::*)(int)

Since the code is ill-formed, the compiler should reject it. 由于代码格式错误,因此编译器应拒绝该代码。 Further, it yields inconsistent results depending on how C::Foo is used; 此外,根据如何使用C::Foo ,它会产生不一致的结果。 we'll look at the inconsistency below. 我们将在下面查看不一致之处。

Please report a bug on Microsoft Connect . 请报告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;

Since tell_me_the_type hasn't been defined, the definition of x is invalid. 由于尚未定义tell_me_the_type ,因此x的定义无效。 The compiler should include the type T in the error it emits. 编译器在其发出的错误中包括类型T Visual C++ 2012 RC reports: Visual C ++ 2012 RC报告:

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

The compiler thinks that C::Foo is of type int (int) . 编译器认为C::Fooint (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)
]

So, C::Foo both is of type int (int) and is not of type int (int) , which violates the principle of noncontradiction . 因此, C::Foo都属于int (int)类型,也不属于int (int)类型,这违反了noncontradiction原理 :-) :-)

So what type is decltype(C::Foo) ? 那么decltype(C::Foo)是什么类型?

It's no type, since using just C::Foo is ill-formed. 这不是类型,因为仅使用C::Foo不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM