The following code compiles on GCC, Clang and MSVC:
struct Foo {
int x;
};
struct Bar: Foo {
decltype(x) z;
};
But this one do not compile with MSVC:
struct Foo {
int x;
};
auto m = [] () {
struct Bar: Foo {
decltype(x) z;
};
};
The error is:
error C4573: the usage of 'Foo::x' requires the compiler to capture 'this' but the current default capture mode does not allow it
Which seems so wrong to me... So I was wondering if this was a MSVC bug or if this was an extension of gcc/clang?
In all cases, is there another way to do what I want without having to prefix x
(decltype(Foo::x)
) inside Bar
?
Note: The original problem arise when writing this answer, where I basically wanted to do the following to avoid having to generate names in macro:
auto something = [] () {
struct: Foo {
decltype(x) z;
auto operator()() { return z; }
} foo;
return foo.operator();
}();
Currently I am using a struct so I have to do something like:
MACRO(Foo, x) my_var;
// or... MACRO(my_var, Foo, x);
auto value = my_var();
But I would like:
auto value = MACRO(Foo, x);