Compilerfehler discards qualifiers


  • Mod

    In dem Fall eine einfache Funktion

    bool XOR(bool lhs, bool rhs)
    {
        return char(lhs) + rhs == 1;
    }
    

    🤡



  • Arcoth schrieb:

    #define XOR(A, B) ( ((A) && !(B)) || (!(A) && (B)) )
    

    dann schon besser

    template<typename T> inline bool XOR(const T& A,const T& B){
    return ((A) && !(B)) || (!(A) && (B));
    }
    

    oder?


  • Mod

    Was soll das inline da? Und überhaupt, warum ein Template?



  • Arcoth schrieb:

    Was soll das inline da? Und überhaupt, warum ein Template?

    Stimmt ist hier unnötig. Wollte möglichst nah an das Macro kommen.



  • Arcoth schrieb:

    In dem Fall eine einfache Funktion

    bool XOR(bool lhs, bool rhs)
    {
        return char(lhs) + rhs == 1;
    }
    

    🤡

    ich habe gerade keinen standard zur hand aber ich glaube, es ist nicht definiert, dass char(true) == 1, lediglich dass char(true) != 0. :p


  • Mod

    ich habe gerade keinen standard zur hand aber ich glaube, es ist nicht definiert, dass char(true) == 1, lediglich dass char(true) != 0.

    Das ist selbstverständlich definiert.

    §4.7/4 schrieb:

    If the source type is bool, the value false is converted to zero and the value true is converted to one.



  • okay, mein fehler, sorry.


  • Mod

    Übrigens: Man sollte das tatsächlich so definieren können:

    bool XOR(bool lhs, bool rhs)
    {
        return char(lhs) ^ rhs; // Edit: Cast eingebaut. Promotion macht den Rest.
    }
    

    Egal welche Repräsentierung integrale Typen haben, gleiche Werte führen zu gleichen Bits, unterschiedliche Werte zu unterschiedlichen Bits. Wahrscheinlich muss man aber noch einen Cast nach char einbauen.



  • siehe auch

    4.5 Integral promotions
    6 A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

    5.12 Bitwise exclusive OR operator [expr.xor]
    exclusive-or-expression:
    and-expression
    exclusive-or-expression ˆ and-expression
    1 The usual arithmetic conversions are performed; the result is the bitwise exclusive OR function of the operands. The operator applies only to integral or unscoped enumeration operands.



  • Was soll das inline da? Und überhaupt, warum ein Template?

    Und so ein Satz von dir ... der doch fuer alles erstmal gleich die Template-/Macro-Maschine anwirft ...


Anmelden zum Antworten