2016-08-19 21 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可以解决问题,因为你已经知道了。