简体   繁体   中英

Deprecated lambda capture in C++20

As proposed in the paper P0409R2

I am expecting the definition of x to be deprecated from C++20 and fail to compile but it seems to work in g++ (GCC) 8.1.0 does anyone know if I am doing anything wrong?

In Visual Studio 2017 is fails to compiler with an error around the definition of y. error C3791: 'this' cannot be explicitly captured when the default capture mode is by copy (=)

Live Code

#include <iostream>

struct  X {
    void  f()
    {
        int  value = 3;

        auto  x = [=] {         // Deprecated from C++20: 
            return  value + g(); 
        };

        auto  y = [=, this] {   // Recommended method from C++20: 
            return  value + g();  // [=] The this pointer will not be captured, so capture with specifying this 
        };
    }

    int g() const
    {
        return  2;
    }
};

int  main()
{
    X().f();
}

First of all that is just a proposal, doesn't automatically mean it will be part of the standard. Second, C++20 is still in the works.

Now, even if the proposal was adopted in C++20 and even if compilers implemented it, it explicitly says that it doesn't propose deprecation of the old way:

We will not consider deprecation in the following survey, since we are only interested in exploring the long-term direction, and deprecation without direction is not all that interesting.

Ok, it looks like it will be indeed deprecated in C++20. But again, as the C++20 standard is't final yet, compilers are implementing it as they go, so gcc hasn't implemented this yet.

As for VS, it looks like the 2017 version hasn't implemented the C++17 capture-this-by-value.

A proposal doesn't mean adoption. That said, reading through the wording and the example, it states:

 [=, this]{ };      // OK, equivalent to [=]

Which seems to indicate that x is allowed. I also don't see explicit mention of this being deprecated. It even mentions the opposite:

We will not consider deprecation in the following survey, since we are only interested in exploring the long-term direction, and deprecation without direction is not all that interesting.

Seems that the paper ended up being approved . Your sample code produces different warnings when compiling in C++ 20 mode. Eg, with GCC 10:

$ g++-10 -c -std=gnu++20 -Wall -o main.o main.cpp
main.cpp: In lambda function:
main.cpp:8:19: warning: implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]
    8 |         auto  x = [=] {         // Deprecated from C++20:
      |                   ^
main.cpp:8:19: note: add explicit ‘this’ or ‘*this’ capture

If I change the switch, the warning is elsewhere:

$ g++-10 -c -std=gnu++1z -Wall -o main.o main.cpp
main.cpp: In member function ‘void X::f()’:
main.cpp:12:23: warning: explicit by-copy capture of ‘this’ redundant with by-copy capture default
   12 |         auto  y = [=, this] {   // Recommended method from C++20:
      |                       ^~~~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM