std::common_type
-
Hallo zusammen,
ich war der Meinung, dass std::common_type bei Integer-Typen den Typ liefert, den auch eine Verknüpfung der Datentypen mit einem Operator (z.B. + ) liefert.
So hatte ich auch dies (http://en.cppreference.com/w/cpp/types/common_type) verstanden:
cppreference.com schrieb:
For arithmetic types, the common type may also be viewed as the type of the (possibly mixed-mode) arithmetic expression such as T0() + T1() + ... + Tn().
Unter Visual Studio 2013 erhalte ich mit diesem Code
#include <type_traits> #include <iostream> int main( void ) { typedef signed char signed_char_t; std::cout << typeid( std::common_type< signed_char_t, signed_char_t >::type ).name() << std::endl; std::cout << typeid( decltype( signed_char_t{ 127 } + signed_char_t{ 127 } ) ).name() << std::endl; }folgende Ausgabe
console schrieb:
signed char
intVerstehe ich std::common_type falsch oder ist die Ausgabe des Programms falsch? Ich hätte erwartet, dass beide Typen gleich sind.
-
Die Ausgabe des Programms ist, soweit ich das sehen kann, richtig, die Beschreibung auf http://http://en.cppreference.com/w/cpp/types/common_type stimmt eben nur in erster Näherung.
std::common_type<A, B>::typeentsprichtdecltype(true ? std::declval<A>() : std::declval<B>())und das liefert in dem Fall etwas anderes, da keine Integral Promotions durchgeführt werden wie beim + Operator...
-
N3797 §5.16/3 schrieb:
Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the second and third operands.
/After those conversions, one of the following shall hold:— The second and third operands have the same type; the result is of that type. If the operands have class type, the result is a prvalue temporary of the result type, which is copy-initialized from either the second operand or the third operand depending on the value of the first operand.
— The second and third operands have arithmetic or enumeration type; the usual arithmetic conversions are performed to bring them to a common type, and the result is of that type.Der zweite Punkt greift nun erst, wenn es zwar zwei Skalare sind, aber unterschiedlichen Typ haben. Das ist hier nicht der Fall.
Btw:
decltype( signed_char_t{ 127 } + signed_char_t{ 127 } ) // <=> decltype( signed_char_t{} + signed_char_t{} )
-
Vielen Dank für Eure Antworten!