Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc7 lambda expression return type without arrow

Tags:

c++

gcc

I wrote the following by accident and got no warning with gcc 8:

auto f = []() bool {};

I've never heard or read anything about the previous syntax. My intent was to write:

auto f = []() -> bool {};

And in this case gcc issues the very useful warning I expected: no return statement.

I did some experiments and GCC accepts the first syntax without warnings or errors since at least GCC 7.1 but not with GCC 6.3. No other compilers seem to accept it. Also, the first syntax is accepted for native types: bool int float and such but not for classes such asstruct A {};

Is this a bug or Is this a new c++17 or c++20 syntax related to, for instance, attributes?

like image 251
Guillaume Gris Avatar asked Jul 17 '18 13:07

Guillaume Gris


1 Answers

It appears to be a fluke. The code (but not the warnings, as you point out) is the same for both, so long as you have GCC 7 or 8. But GCC 6 doesn't accept it, nor any other compiler I tried.

It does not appear to be valid C++. It definitely wasn't added for C++20, because GCC 7 did not add any C++20 features (though 8 did add some).

like image 166
John Zwinck Avatar answered Oct 19 '22 09:10

John Zwinck