Compilerfehler discards qualifiers



  • Jetzt kommt:

    GroupRingElement operator+ (const GroupRingElement& a, const GroupRingElement& b)
    {
        GroupRingElement result;
        for (auto g: G)
        {
            result[g] = a.at(g) ^^ b.at(g);
        }
        return result;
    }
    
    C:\GroupRing\GroupRing\main.cpp|121|error: expected primary-expression before '^' token|
    

  • Mod

    a.at(g) ^^ b.at(g)
    

    Soll das ein Scherz sein? Wenn du ein logisches XOR haben möchtest, nutze

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


  • fdgf schrieb:

    Jetzt kommt:

    GroupRingElement operator+ (const GroupRingElement& a, const GroupRingElement& b)
    {
        GroupRingElement result;
        for (auto g: G)
        {
            result[g] = a.at(g) ^^ b.at(g);
        }
        return result;
    }
    
    C:\GroupRing\GroupRing\main.cpp|121|error: expected primary-expression before '^' token|
    

    nein nein, was du willst ist vermutlich:

    GroupRingElement::const_iterator a_element = a.find(g), b_element = b.find(g);
    if(a_element == a.end() || b_element  == b.end()
    {
        // ...
    }
    result[g] = *a_element && !*b_element || !*a_element && *b_element;
    


  • Arcoth schrieb:

    a.at(g) ^^ b.at(g)
    

    Soll das ein Scherz sein? Wenn du ein logisches XOR haben möchtest, nutze

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

    schlechte idee.

    quizfrage: wie hoch sind a und b?

    #define XOR(A, B) ( ((A) && !(B)) || (!(A) && (B)) )
    int a = 0, b = 2;
    XOR(a++, --b);
    int f()
    {
        static int c = 0;
        return c++;
    }
    XOR(++b, f())
    

  • Mod

    Edit: Ahh, ich verstehe! Hast Recht.


  • 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