0

So long story short I saw my teacher write a function that had a "?" as a non particular example of how he described that in the future we would make our code shorter and better. I don't really know what he did, I tried searching for it in code examples but I only found this line of code that resembles what he had done. My question being: Is there a name for this so I can investigate further? or Is it a combination of things that makes this shorter "if" like function.

MATCHER(IsFalse, negation ? "is true" : "is false") { return !static_cast<bool>(arg); }
Nate Eldredge
  • 36,841
  • 4
  • 40
  • 60
RekusuM
  • 11
  • 1
  • 1
    This is called the *ternary* operator. – Brady Dean Sep 19 '21 at 01:02
  • 8
    Side note: Shorter does not always equal better. – Johnny Mopp Sep 19 '21 at 01:05
  • Also see https://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-ternary-operator – Nate Eldredge Sep 19 '21 at 01:09
  • 1
    If the professor didn't tell you the name of the thing they showed you, that's an area they need to improve on. With that said, showing a class the ternary operator for the first time always explodes a couple heads, and not in a good way. Usually in the 'I may need to switch majors' kind of way. It's odd. Some students just do not get it. – sweenish Sep 19 '21 at 01:12
  • 2
    The normative term is "the conditional expression". It is referred to colloquially as the ternary operator because it's the only built-in expression taking three operands. – StoryTeller - Unslander Monica Sep 19 '21 at 01:23

0 Answers0