[英]decltype and member function (not pointer) type
struct C
{
int Foo(int i) { return i; }
typedef decltype(C::Foo) type;
};
由於不存在成員函數類型(不存在,不存在?),因此我期望C::type
為int (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::Foo
是int (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.