When performing SFINAE on an arbitrary type, it's often necessary to cast the result of an expression to void
. I've seen two ways of doing this; a cast to void:
(void)(expr) // or static_cast<void>(expr)
Or, alternatively, using the comma operator with a void prvalue RHS:
(expr), void()
It's my understanding that in both cases expr
is evaluated (for well-formedness, in a non-evaluated context) and the result (or result type, in a non-evaluated context) discarded; it is not possible in either case for even a pathological class T
to override T::operator void()
or operator,(T, void)
. (See: Why is "operator void" not invoked with cast syntax?, What does the 'void()' in 'auto f(params) -> decltype(..., void())' do?).
That said, are these two idioms equivalent, or are there any circumstances under which one should be preferred to the other (possibly with nonstandard compilers)? If not, are there any reasons (e.g. understandability) to prefer one over the other?