2
下面的代碼編譯的GCC,鏘和MSVC:decltype使用父母的成員不是內部拉姆達工作MSVC
struct Foo {
int x;
};
struct Bar: Foo {
decltype(x) z;
};
但是這一次不MSVC編譯:
struct Foo {
int x;
};
auto m = []() {
struct Bar: Foo {
decltype(x) z;
};
};
的錯誤是:
錯誤C4573:使用'Foo :: x'需要編譯器捕獲'this',但當前的默認捕獲模式不允許使用它
對我來說這似乎是錯誤的...所以我想知道這是一個MSVC錯誤還是這是gcc/clang的擴展?
在所有情況下,有沒有另外一種方法可以做到我想要的,而不必在Bar
內加上x
(decltype(Foo::x)
)的前綴?
注:寫this answer,當原始出現問題,我基本上是想做到以下幾點,以避免產生在宏名稱:
auto something = []() {
struct: Foo {
decltype(x) z;
auto operator()() { return z; }
} foo;
return foo.operator();
}();
目前我使用一個結構等等我必須做一些事情,如:
MACRO(Foo, x) my_var;
// or... MACRO(my_var, Foo, x);
auto value = my_var();
但我想:
auto value = MACRO(Foo, x);
您使用的是什麼版本的Visual Studio? VS2015(v140)用lambda編譯第二個代碼,沒有問題。 –
@DeanSeo它是VC++ 19.00.23506,[rextester.com]上的版本(http://rextester.com/EECOI71008)。 – Holt