3

I'm trying to find a way to replicate the behavior in hlsl where the ternary operator actually selects the bits that evaluate the condition, i.e instead of evaluating to bool they evaluate to bool4. Something like the following code:

float4 condition = float4(1, 0, 1, 0);
float4 a = float4(2, 2, 2, 2);
float4 b = float4(3, 3, 3, 3);

float4 c = condition > 0 ? a : b;

c becomes (2, 3, 2, 3)

However, the C++ ternary operator needs a bool and selects only one of the values. I think this is a legit scenario where overloading the ternary operator is desired.

Is there absolutely no way to get this behavior in C++?

UPDATE [23/12/2019] Apparently I'm not the only one with this issue, and for the same reason as explained in this paper http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0917r3.pdf

We'll probably get it at some point for the bleeding edge of C++, better late than never I guess.

RedOrav
  • 953
  • 9
  • 21
  • Btw, even if you could overload `operator ?:` as desired, it's debatable at least whether that would be a good idea. – Baum mit Augen Jan 16 '17 at 12:03
  • I don't understand the question. Where is the `bool4`? What are "the bits that evaluate the condition"? In what way does the above code not already evaluate to a `float4` (which _is_ "one of the values" but presumably encapsulates the list `2, 3, 2, 3`)? Basically I'm pretty sure that what you want is already how the _conditional operator_ works. Perhaps present your [MCVE] that demonstrates otherwise and asks a practical question about a piece of C++ code. – Lightness Races in Orbit Jan 16 '17 at 12:05
  • @LightnessRacesinOrbit it's a normal operator usage in hlsl. Not understanding it is not a valid reason for dismissing it. Basically it does four ternary operators in one, as it's a way to exploit the simd capabilities of the machine. Not being able to do it in C++ limits the ability to do this. – RedOrav Jan 16 '17 at 12:07
  • I didn't "dismiss it", I asked you for clarification. So, you are looking for a sort of map-conditional that examines each "element" in turn? That behaviour, on `?:`, would be extremely confusing/surprising to a C++ developer. So no I don't think it would be a good idea. Write a function, instead. C++ is not a shader language; indeed, this is why we have shader languages. – Lightness Races in Orbit Jan 16 '17 at 12:08
  • @LightnessRacesinOrbit Yes, I'm looking for a way for the ternary operator to examine each element in turn, evaluate the condition, and then select the result that corresponds to its position in the vector. Look at how a and b are "mixed" in c, where it has selected part of a and part of b. – RedOrav Jan 16 '17 at 12:10
  • 1
    Refer to my previous comment — that would be a very surprising thing to see in C++ code, and would be a very bad use of operator overloading. Fortunately, you can't do it anyway. :) – Lightness Races in Orbit Jan 16 '17 at 12:11
  • @LightnessRacesinOrbit I guess we need to disagree, because I'm trying to replicate the hlsl behavior in exactly the same way. Applying the conditional operator to a vector of 4 components makes no sense unless it works in the way I'm describing it. What does if(vector > 0) mean? The ternary operator allows you to use the vector as a select mask, which is perfectly doable in SIMD. – RedOrav Jan 16 '17 at 12:14
  • Yes I now understand what you're trying to do. I'm saying that you cannot change what `?:` does, and that it is a good thing that you cannot. C++ is not HLSL, and people reading your code will not expect it to be. You should write a function with a good name. – Lightness Races in Orbit Jan 16 '17 at 12:16
  • Fair enough :) I rest my case for the purposes of this discussion. I still think it's a good idea and not as surprising as you make it seem. – RedOrav Jan 16 '17 at 12:19

0 Answers0