Typkonvertierung von konstanten Variablen in Templates



  • Hallo!

    Mal eine kleine Frage:

    Macht der Compiler aus

    template <class T>
    T add42(T x)
    {
        return x + T(42);
    }
    
    float y = add42(1234.0f);
    
    float add42(float x)
    {
        return x + float(42); // Type conversion at run time
    }
    
    float y = add42(1234.0f);
    

    oder

    float add42(float x)
    {
        return x + 42.0f; // Type conversion at compile time
    }
    
    float y = add42(1234.0f);
    

    ? Ist das irgendwie standardisiert?

    Grüße



  • Bloops schrieb:

    Macht der Compiler aus

    Theoretisch ersteres, praktisch ist dies vermutlich vom Compiler abhängig, wie gut er optimiert.


Anmelden zum Antworten