2016-08-19 29 views
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內加上xdecltype(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); 
+1

您使用的是什麼版本的Visual Studio? VS2015(v140)用lambda編譯第二個代碼,沒有問題。 –

+0

@DeanSeo它是VC++ 19.00.23506,[rextester.com]上的版本(http://rextester.com/EECOI71008)。 – Holt

回答

1

看來這是一個編譯器錯誤。

這是VC++ 19.00.23506,在rextester.com

最新的VC++編譯器的版本,其版本是19.00.24213,有固定的這個問題。

顯然使用decltype裏面的lambda在VC++裏帶了some issues detecting type from out of scope

明確地告訴編譯器Foo::x可以解決問題,因爲你已經知道了。