The template class std::common_type
calculates a common type to a variadic type list. It is defined using the return type of the ternary operator x:y?z
recursively. From that definition it is not obvious to me, whether calculating a std::common_type<X,Y>
is associative, i. e. whether
using namespace std;
static_assert( is_same<common_type< X, common_type<Y,Z>::type >::type,
common_type< common_type<X,Y>::type, Z >::type>::value, "" );
will never throw a compile-time error for all types X
, Y
and Z
for which the is_same<...>
expression is valid.
Please note, that I'm NOT asking whether
static_assert( is_same<common_type<X,Y>::type,
common_type<Y,X>::type>::value, "" );
will ever fire. It will obviously not. The above is a whole different question.
Please note also, that the specification of std::common_type
slightly changed in C++14 and will probably change again in C++17. So the answers may be different for different versions of the standard.
(X,(Y,Z))
has a common type, but(X,Y)
, and hence(X,Y),Z
, does not? Or do you want them both to resolve to a type, but different ones. 'Cause I'm pretty stuck trying to construct a case for the latter, and strongly suspect it isn't possible; any two chains of conversions will cause an ambiguity.common_type
types are valid, butis_same
is false.operator==
in a way that is not consistent with logic.operator==
argument: a double cast down to a float may equal another float, but a float cast up to a double can easily fail equality. (Using this impl. warrants documentation, but casting both to LCM (float here) or both to GCM (double here) to enforce assoc. is edge case spackle.)