简体   繁体   中英

lambda capture initializers warning for c++11

There is c++ code like this:

auto func = [=, vec1=std::move(vec)]() {
                   printf("%x  %x  %x\n", p,vec1.data(), vec.data());
           };

when I compile it with -std=c++11 , the compiler print warning

lambda capture initializers only available with -std=c++14 or -std=gnu++14 ,

But the code can run correctly, so should I need process this warnning .

so should I need process this warnning.

You should fix the code so that there is no warning.

There are two options: Compile in C++14 mode or later standard version where lambda capture initializers are allowed, or don't use vec1=std::move(vec) lambda initializer.

These code need cross compile, so I need consider the plateform support c++14 or not, this is a history problem, so could not add c++14 directly

So choose the latter option. Don't use C++14 features if your target platform doesn't support it.

If you need to move into a capture, you can achieve it in C++11 using std::bind :

auto func = std::bind(
    [=](const decltype(vec)& vec1) {
       printf("%x  %x  %x\n", p,vec1.data(), vec.data());
   },
   std::move(vec)
);

PS %x format specifier requires that the argument is unsigned int But the return type of std::vector<T>::data is not unsigned int , but T* , so the behaviour will be undefined. %p is for pointers.

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