2

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);
Community
  • 1
  • 1
Holt
  • 36,600
  • 7
  • 92
  • 139
  • 1
    What version of Visual Studio do you use? VS2015(v140) compiles the second code with lambda with no problems. – Dean Seo Aug 19 '16 at 10:10
  • @DeanSeo It is VC++ 19.00.23506, the version on [rextester.com](http://rextester.com/EECOI71008). – Holt Aug 19 '16 at 11:19

1 Answers1

1

It seems it is a compiler bug.

It is VC++ 19.00.23506, the version on rextester.com

The latest VC++ compiler, the version of which is 19.00.24213, has fixed that issue.

Apparently using decltype inside lambda in VC++ had brought some issues detecting type from out of scope.

Telling the compiler explicitly Foo::x can be a work around, as you have already figured it out.

Dean Seo
  • 5,486
  • 3
  • 30
  • 49