short anderes Verhalten als int, longlong und co



  • Guten Abend,
    gleich zu Anfang, mir ist kein treffender Thread-Titel eingefallen...

    Beim Durchforsten der STD viel mir heute auf, dass std::to_string für int, long, long long und die jeweils Vorzeichenlose Variante definiert ist, aber nicht für short. Also habe ich etwas mit short experimentiert und folgendes als Beispiel für euch:

    #include <iostream>
    
    struct Short_Int{	
    	Short_Int(const short Short){
    		std::cout<<"A Short \n";
    	}
    
    	Short_Int(const int Int){
    		std::cout<<"A Int \n";
    	}	
    };
    
    struct Long_LongLong{	
    	Long_LongLong(const long Long){
    		std::cout<<"A Long \n";
    	}
    
    	Long_LongLong(const long long LongLong){
    		std::cout<<"A LongLong \n";
    	}	
    };
    
    int main(int argc, char** argv) {
    
    	//	a)
    	short s=30000;
    	unsigned short us=60000;
    
    	Short_Int s_i(s);			//Ausgabe: "A Short"
    	Short_Int us_i(us);			//Ausgabe: "A Int"	-> Kein Fehler?!
    
    	//	b)
    	long l=2000000000;
    	unsigned long ul=4000000000;
    
    	Long_LongLong l_ll(l);		//Ausgabe "A Long"
    	Long_LongLong ul_ll(ul);	//Fehler: "Call of overloaded 'Long_LongLong(long unsigned int&)' is ambiguous"	
    
    	return 0;
    }
    

    Eigentlich hätte ich bei a) und b) das gleiche Verhalten erwartet, nun frage ich mich/euch warum das so umgesetzt wurde?

    Vielen Dank und schönen Abend,
    John.



  • Ganz einfach: das eine ist eine integral promotion.
    (unsigned) short hat einen kleineren Rank als int weswegen es via Promotion zu einem int konvertiert werden kann.
    unsigned short zu short hingegen wäre eine integral conversion.
    Eine promotion ist besser als eine conversion, weswegen int ausgewählt wird.
    Beim zweiten Fall ist das hingegen beides eine conversion, deswegen ambig.



  • Nathan schrieb:

    Beim zweiten Fall ist das hingegen beides eine conversion.

    Und warum ist "unsigned short -> int" eine integral conversion, aber "unsigned long -> long long" nicht ?
    Sind doch beides integral typen.



  • DarkShadow44 schrieb:

    Nathan schrieb:

    Beim zweiten Fall ist das hingegen beides eine conversion.

    Und warum ist "unsigned short -> int" eine integral conversion, aber "unsigned long -> long long" nicht ?
    Sind doch beides integral typen.

    Du meinst wohl promotion. Und deshalb:

    §4.5/1 schrieb:

    A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

    integral promotion ist definiert als die Konvertierung zu int*. Alles andere ist eine Konversion, nur ggf. eine "verlustfreie Konversion".
    Hätte den Paragraphen doch zitiert lassen sollen...

    *Außer für char16_t, char32_t und wchar_t, da ist das die Konvertierung zum ersten Typen, der alles aufnehmen kann; steht im Abschnitt dadrunter.


Anmelden zum Antworten