Apparently, this ternary expression with void()
as one argument compiles:
void foo() {}
//...
a == b ? foo() : void();
Is void()
a valid expression by the standard, or is it just a compiler thing? If it's valid, then what kind of expression is it?
if(a==b){ foo(); }
is clearer and has the same effect. – Ravishmentvoid
. (Although I will be the first to say that me not being able to think of one doesn't mean it can't exist) – Ravishmentvoid
came from a template type and this expression is argument to another function call – Tsimshianvoid()
as an argument. – Ravishmentvoid()
: with SFINAE:template <typename T> auto bar(T t) -> decltype(func(t), void())
(return type isvoid
) Or to handle evil overload of comma operator:(f1(t), void(), f2(t))
. – Philatelyvoid()
is not useful. I know it is very useful in metaprogramming (that's whystd::void_t
is a thing). I'm arguing specifically abouta==b?foo():void()
. If you were to put this expression indecltype
, for instance, then you don't really need the ternary operator, since neither the boolean expression will be checked, norfoo()
actually be called. Same for handling of comma operator. – Ravishmentdecltype(a == b ? foo() : void())
versusdecltype(a==b, void(), foo(), void())
(former is shorter even if I prefer the later). – Philately