繁体   English   中英

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

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

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

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

由于不存在成员函数类型(不存在,不存在?),因此我期望C::typeint (int)

但是以下内容无法使用Visual C ++ 2012 RC进行编译:

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

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

代码格式不正确:只能使用几种方法来使用成员函数名称(例如C::Foo ),但这不是其中一种(可以在C ++语言中找到有效用法的完整列表)标准,请参见C ++ 11§5.1.1/ 12)。

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

由于代码格式错误,因此编译器应拒绝该代码。 此外,根据如何使用C::Foo ,它会产生不一致的结果。 我们将在下面查看不一致之处。

请报告Microsoft Connect上的错误。 或者,让我知道,我很乐意报告该问题。


如果您有一个类型,但不知道该类型是什么,则可以使用某种类型的名称来查找类型名称,该方式会导致编译器发出错误。 例如,声明一个类模板而不定义它:

template <typename T>
struct tell_me_the_type;

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

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

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

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

编译器认为C::Fooint (int)类型的。 如果是这种情况,那么编译器应接受以下代码:

template <typename T>
struct is_the_type_right;

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

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

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

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

因此, C::Foo都属于int (int)类型,也不属于int (int)类型,这违反了noncontradiction原理 :-)

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

这不是类型,因为仅使用C::Foo不正确。

暂无
暂无

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

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