Most C++ programmers are familiar with the ternary operator:
x = (y < 0) ? 10 : 20;
However, they don’t realize that it can be used as an lvalue:
(a == 0 ? a : b) = 1;
which is shorthand for
if (a == 0)
a = 1;
else
b = 1;
Use with caution 🙂
More Related Contents:
- What is wrong with the following code which calculates a sorted array [closed]
- What is the difference between these cin functions?
- How approximation search works
- Officially, what is typename for?
- Fastest method of screen capturing on Windows
- Differences between unique_ptr and shared_ptr [duplicate]
- Why can’t I have a non-integral static const member in a class?
- Avoiding Circular Dependencies of header files [duplicate]
- CreateProcess from memory buffer
- What does the >?= operator mean?
- Call main() itself in c++?
- What is the best way to take screenshots of a Window with C++ in Windows?
- What’s the purpose of using braces (i.e. {}) for a single-line if or loop?
- Identifying primitive types in templates
- Use WM_COPYDATA to send data between processes
- Advantages of std::for_each over for loop
- Why is CLOCKS_PER_SEC not the actual number of clocks per second?
- How can I get the path of a Windows “special folder” for a specific user?
- What’s the difference between a const member function and a non-const member function?
- Using pthread in c++
- Can I use std::async without waiting for the future limitation?
- variable or field declared void
- How to enforce the ‘override’ keyword?
- Scope with Brackets in C++
- Static and Dynamic/Shared Linking with MinGW
- std::remove_const with const references
- Creating JSON arrays in Boost using Property Trees
- How to detect Text Area from image?
- How to test whether class B is derived from template family of classes
- What’s the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?