Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C++=== Unlike in [[C (programming language)|C]], the precedence of the {{code|?:}} operator in [[C++]] is the same as that of the assignment operator ({{code|1==}} or {{code|1=OP=}}), and it can return an lvalue.<ref>{{cite web |url=http://en.cppreference.com/w/cpp/language/operator_precedence#Notes |title=C++ Operator Precedence |at=section: "Notes" |website=en.cppreference.com }}</ref> This means that expressions like {{code|1=q ? a : b = c}} and {{code|1=(q ? a : b) = c}} are both legal and are parsed differently, the former being equivalent to {{code|1=q ? a : (b = c)}}. In [[C++]] there are conditional assignment situations where use of the ''if-else'' statement is impossible, since this language explicitly distinguishes between [[initialization (programming)|initialization]] and [[assignment (programming)|assignment]]. In such case it is always possible to use a function call, but this can be cumbersome and inelegant. For example, to pass conditionally different values as an argument for a constructor of a field or a base class, it is impossible to use a plain ''if-else'' statement; in this case we can use a '''conditional assignment expression''', or a function call. Bear in mind also that some types allow initialization, but do not allow assignment, or even that the assignment operator and the constructor do totally different things. This last is true for reference types, for example: <syntaxhighlight lang="cpp" highlight="16"> #include <iostream> #include <fstream> #include <string> int main(int argc, char *argv[]) { std::string name; std::ofstream fout; if (argc > 1 && argv[1]) { name = argv[1]; fout.open(name.c_str(), std::ios::out | std::ios::app); } std::ostream &sout = name.empty() ? std::cout : fout; sout << "Hello, world!\n"; return 0; } </syntaxhighlight> In this case, using an '''if-else''' statement in place of the {{code|?:}} operator forces the target of the assignment to be declared outside of the branches as a [[Pointer (computer programming)|pointer]], which can be freely rebound to different objects. <syntaxhighlight lang="cpp" highlight="16"> std::ostream* sout = &fout; if (name.empty()) { sout = &std::cout; } *sout << "Hello, world!\n"; </syntaxhighlight> In this simple example, the {{code|sout}} pointer can be initialized to a default value, mitigating the risk of leaving pointers uninitialized or [[Null pointer|null]]. Nevertheless, there are cases when no good default exists or creating a default value is expensive. More generally speaking, keeping track of a nullable pointer increases cognitive load. Therefore, only conditional assignment to a reference through the {{code|?:}} operator conveys the semantics of ''Initializing a variable from only one of two choices based on a predicate'' appropriately. Furthermore, the conditional operator can yield an lvalue, i.e. a value to which another value can be assigned. Consider the following example: <syntaxhighlight lang="cpp" line highlight="8"> #include <iostream> int main(int argc, char *argv[]) { int a = 0; int b = 0; (argc > 1 ? a : b) = 1; std::cout << "a: " << a << " b: " << b << '\n'; return 0; } </syntaxhighlight> In this example, if the boolean expression {{code|argc > 1}} yields the value {{code|true}} on line 8, the value {{code|1}} is assigned to the variable {{code|a}}, otherwise the value {{code|1}} is assigned to the variable {{code|b}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)