相当于C++中的Lua"和/或"?

pig*_*d10 3 c++ lua

在Lua中,你可以这样做:

foo = a and b or c and d or e
Run Code Online (Sandbox Code Playgroud)

这相当于(至少我相信它相当于):

local foo = nil
if a then
foo = b
elseif c then
foo = d
else
foo = e
end
Run Code Online (Sandbox Code Playgroud)

在C++中是否有与此类似或相似的内容?

Hak*_*rce 6

我想这就是你想要的:

foo = a ? b : (c ? d : e );
Run Code Online (Sandbox Code Playgroud)


rob*_*ert 5

三元运算符.它具有有趣的优先级,所以总是将它括起来是很好的做法.

bool foo = ( a ? b : ( c ? d : e ) )
Run Code Online (Sandbox Code Playgroud)

请注意,这只是工作,如果b,d以及e可减少对同一类型.如果adouble,dfloateint,你的结果将始终被转换为double.