Other examples are really good. There is another option, although I would not recommend it (for completeness only).
GCC has an extension called โLegends with Omitted Operands,โ which basically looks like this:
x = a ?: b;
which is the same (in simple cases like yours, see below for more information):
x = a ? a : b;
Only less portable. So you could write:
std::wstringstream outstream; outstream << (prop.m_pwszOriginalVolumeName ?: L"null") << L";" << (prop.m_pwszSnapshotDeviceObject ?: L"null") << L";" << (prop.m_pwszOriginatingMachine ?: L"null") << L";"
But, as I said, I would not want to recommend this, I would use a helper function, as the other answers mention.
In fact, this is the case when it is performed differently than the usual triple if and if there are side effects when evaluating a . On the page:
In this simple case, the ability to omit the middle operand is not particularly useful. When it becomes useful, it is when the first operand or can (if it is a macro argument) contains a side effect. then repeating the operand in the middle will perform a side effect twice. Lowering the middle operand uses the already calculated value without the undesirable consequences of reprogramming.
See http://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
source share