Why won't this C ++ lamba compilation get compiled?

Why this fails to compile:

int myVar = 0;
myVar ? []()->void{} : []()->void{};

with the following msg error:

Error 2 of error C2446: ':': there is no conversion from 'red_black_core :: `anonymous-namespace' :: <lambda1> 'to red_black_core :: anonymous-namespace :: <lambda0>

Although this is true:

void left()
{}
void right()
{}

int myVar = 0;
myVar ? left() : right();
+3
source share
5 answers

Rules for conditional statement in n3225 project say at some point

prvalue. , (, cv-quali-fi) , ( ) (13.3.1.2, 13.6). , . , , .

(, ) , , . , , , a ? b : c operator?(a, b, c) ( ). , operator?, ( )

T, T - , ,

T operator?(bool, T , T );

, T void(*)(). , - , .

- - const, , , . , , , , .

- , , , operator? - . opreator , , , .

, GCC . , , , - , , , - ( ). .

+5

?: , . , . , , ?: , , .

, , , . , , , ?: Void.

void left()
{}
void right()
{}

int myVar = 0;
myVar ? left() : right();

int myVar = 0;
myVar ? [](){}() : [](){}();

extra() - .

,

compiler_deduced_type var;
if (myVar)
    var = [](){};
else
    var = [](){};

, . , .

EDIT:

- . lambdas . compiler_deduced_type (*)(). , , MSVC , , lambdas. , GCC , MSVC - . GCC , MSVC.

+9

- . , , .

, lambdas , , , lambda MSVC.

, , lambdas , , .

+3

GCC 4.5.2.

, ( /) ++ 0x, lambda?

+2

. . , , ++ 0x .

Edit: ! , ++ 0x, . .

- ++ 0x, ++ 0x, , , . ++ 03, , .

:

Also note that you probably did not write what you really wanted to write. []()->void{}- lambda. []()->void{}()performs a lambda and evaluates its result. Depending on what you are doing with this result, your problem may be that the result of calling your lambda is void, and you cannot do much with help void.

0
source

Source: https://habr.com/ru/post/1785429/


All Articles