三元运算符不适用于lambda函数

时间:2022-09-28 22:30:58

I am assigning to a std::function<double()> a lambda expression. This snippet works

我正在为std :: function 分配一个lambda表达式。这个片段有效 ()>

if(fn_type==exponential)
    k.*variable = [=,&k](){ return initial*exp(-k.kstep*par); };
else
    k.*variable = [=,&k](){ return initial*pow(k.kstep, par); };

whereas if I want to use the ternary operator

而如果我想使用三元运算符

k.*variable = (fn_type==exponential ? [=,&k](){ return initial*exp(-k.kstep*par); } : [=,&k](){ return initial*pow(k.kstep, par); });

I get the following error:

我收到以下错误:

error: no match for ternary ‘operator?:’ in <awfully long template error, because this whole thing is in a class defined in a function...>

Is this a gcc bug (I'm using 4.7.2)? Otherwise why is there this limit in the standard?

这是一个gcc bug(我使用的是4.7.2)?否则为什么标准中有这个限制?

1 个解决方案

#1


18  

The second and third operands of the conditional operator must have the same type or there must be some common type to which they can both be converted that the compiler can figure out. There are only a handful of conversions that the compiler will consider.

条件运算符的第二个和第三个操作数必须具有相同的类型,或者必须存在一些可以转换的公共类型,编译器可以找到它们。编译器只会考虑少数转换。

Your two lambda expressions have different types, and there is no common type to which they can both be converted (conversions to user-defined types, like std::function<double()>, cannot be considered because there are potentially an infinite number of valid target types).

您的两个lambda表达式具有不同的类型,并且没有可以转换它们的公共类型(转换为用户定义的类型,如std :: function ,因为可能存在无穷大的数字,所以无法进行转换有效的目标类型)。 ()>

You can directly convert each of the operands to std::function<double()>:

您可以直接将每个操作数转换为std :: function ()>

k.*variable = fn_type==exponential
    ? std::function<double()>([=,&k](){ return initial*exp(-k.kstep*par); })
    : std::function<double()>([=,&k](){ return initial*pow(k.kstep, par); });

But really, it's cleaner with the if/else.

但实际上,使用if / else更清洁。

#1


18  

The second and third operands of the conditional operator must have the same type or there must be some common type to which they can both be converted that the compiler can figure out. There are only a handful of conversions that the compiler will consider.

条件运算符的第二个和第三个操作数必须具有相同的类型,或者必须存在一些可以转换的公共类型,编译器可以找到它们。编译器只会考虑少数转换。

Your two lambda expressions have different types, and there is no common type to which they can both be converted (conversions to user-defined types, like std::function<double()>, cannot be considered because there are potentially an infinite number of valid target types).

您的两个lambda表达式具有不同的类型,并且没有可以转换它们的公共类型(转换为用户定义的类型,如std :: function ,因为可能存在无穷大的数字,所以无法进行转换有效的目标类型)。 ()>

You can directly convert each of the operands to std::function<double()>:

您可以直接将每个操作数转换为std :: function ()>

k.*variable = fn_type==exponential
    ? std::function<double()>([=,&k](){ return initial*exp(-k.kstep*par); })
    : std::function<double()>([=,&k](){ return initial*pow(k.kstep, par); });

But really, it's cleaner with the if/else.

但实际上,使用if / else更清洁。