C++ % " ?



  • @Konstantin-Tokarev
    LOL! Wait!! You can overload the C comma operator? OMG!


  • Qt Champions 2017

    @JonB said in C++ % " ?:

    You can overload the C comma operator?

    Of course you can. There are only a handful of operators that can't be overloaded; the scope resolution ::, the member access . the pointer to member access .* and the short if ?:



  • @kshegunov
    Haven't seen you in a while!

    I was thinking earlier: why couldn't/didn't C++ allow defining your own operators then? I have used languages where you can. By the time Qt is having QString override an operator to add some convenient behaviour, it's plain confusing that it picks an existing "modulus" symbol to do it :( If you're going to change the meaning so much, you only really need to be able to specify the precedence and you might as well be free to pick your own operator characters.

    Going back to the , operator. I didn't realise it was a true operator, that means you have to be consistent about the types on each side, I didn't think the left-hand side mattered, I thought it just returned the right-hand side.


  • Qt Champions 2017

    @JonB said in C++ % " ?:

    @kshegunov
    Haven't seen you in a while!

    many-a-work.

    I was thinking earlier: why couldn't/didn't C++ allow defining your own operators then?

    It could've, I imagine it's a conscious choice not to make it even more confusing.

    Going back to the , operator. I didn't realise it was a true operator

    Yes, albeit a low-priority one.


  • Moderators

    @JonB said in C++ % " ?:

    @Konstantin-Tokarev
    LOL! Wait!! You can overload the C comma operator? OMG!

    There is hardly a thing one can't change/overload in c++.
    But you do, the operation should be somewhat related to the original. Otherwise you'll end up with confusion and lost development time.

    I'm looking at you boost 0_1537780536073_34207ed0-bfd8-4865-9798-02d5853acddc-image.png



  • @J.Hilk
    LOL, I don't know about Boost. But how's about the subject of this post: how is this QString % operator related to original modulus?



  • Hope you cannot overload ';' That would probably cause the ultimate confusion.



  • @koahnig ; is not an operator. I believe it is a "statement separator" (whereas Pascal had it as a "statement terminator"... or is it the other way round?).



  • @JonB

    Thanks for clarification, I am so happy about that ;)

    However, with the ancient FORTRAN they discussed a COME FROM statement
    Therefore, you never know what somebody will come up with. At least FORTRAN did not require a "statement separator" nor a "statement terminator". The logical end of aline was the end of the punching card after 80 chars respectively you had to subtract 8 digits for the line number.



  • @JonB said in C++ % " ?:

    @J.Hilk
    LOL, I don't know about Boost. But how's about the subject of this post: how is this QString % operator related to original modulus?

    modulus has no established meaning for strings, and % looks somewhat similar to + :)



  • Though you should better define QT_USE_QSTRINGBUILDER and simply use + everywhere instead of %



  • @Konstantin-Tokarev said in C++ % " ?:

    Though you should better define QT_USE_QSTRINGBUILDER and simply use + everywhere instead of %

    Ooohhh, the plot thickens...!


  • Moderators

    @JonB Qt has ome confusing operaters as well.

    for example take QVectors +=

    QVector<T> QVector::operator+(const QVector<T> &other) const
    

    I would asume this to be Vector addtion

    QVector v1{a,b,c};
    QVector v2{d,e,f};
    
    //What one would expect
    v1 += v2; -> {a+d, b+e, c+f}
    
    //what one gets
    v1 +=v2; _> {a,b,c,d,e,f}
    

    I mean, this technically makes sense. QVector is to generic to have this operation, thats why there is QVector2D, 3D, 4D etc

    It still confused me the first time I used it. I did not expect += to be equal to .append() or <<;

    In this paticular example I have would prefere a missing += operator :-).


  • Qt Champions 2018

    @J.Hilk To be honest I would be rather confused if it would do

    v1 += v2; -> {a+d, b+e, c+f}
    

    A vector is not a number or something, it is a container. Or is it because it is called "vector" and you expect it to behave like a vector in math? :-)



  • @jsulm said in C++ % " ?:

    @J.Hilk To be honest I would be rather confused if it would do

    v1 += v2; -> {a+d, b+e, c+f}
    

    A vector is not a number or something, it is a container. Or is it because it is called "vector" and you expect it to behave like a vector in math? :-)

    That is debatable and apparently dependent on your background. The foprm you find confusing would be the most logical version for me.


  • Moderators

    @jsulm I used to use only QList as a container, and one day I needed to do Vector operations. In my youthfull naiveté I assumed QVector = linear algebra vector.
    I learned quickly thats not the case 🤷

    Ever since, I prefere using QVector over QList.😉


  • Qt Champions 2018

    @koahnig Yes, that's true.
    @J-Hilk I'm actually wondering why it is called vector? Something like "QDynamicArray" would be less confusing.



  • @jsulm

    In the mathematical sense it is alwways a vector, but in spaces with different dimensions. All operators are bascially identical, therefore "real" engineers can accept this. Personally I doubt that the definition of basically changing the space dimension during an addition is conform with pure mathematics. On the other hand you never know what they think/thoght in those areas ;)


  • Qt Champions 2017

    @jsulm said in C++ % " ?:

    I'm actually wondering why it is called vector?

    In linear (and higher) algebra, a vector is an ordered set, or more precisely (and formally speaking) - it is the element of the linear space defined over a field; usually the field of real or complex numbers. Very similar to what you call a vector in programming.

    It's just convenience that in Qt you have the + defined to mean merging of two arrays, and not the mathematical operation, as you don't regularly require the mathematical properties of a vector; it's just not so useful in everyday life.


  • Moderators

    @J.Hilk said in C++ % " ?:

    @JonB Qt has ome confusing operaters as well.

    for example take QVectors +=

    QVector<T> QVector::operator+(const QVector<T> &other) const
    

    I would asume this to be Vector addtion

    QVector v1{a,b,c};
    QVector v2{d,e,f};
    
    //What one would expect
    v1 += v2; -> {a+d, b+e, c+f}
    
    //what one gets
    v1 +=v2; _> {a,b,c,d,e,f}
    

    I mean, this technically makes sense. QVector is to generic to have this operation, thats why there is QVector2D, 3D, 4D etc

    It still confused me the first time I used it. I did not expect += to be equal to .append() or <<;

    In this paticular example I have would prefere a missing += operator :-).

    Wait till you try + or += on QJsonArray ;)

    QJsonArray a1{1, 2, 3};
    QJsonArray a2{4, 5, 6};
    
    a1 += a2;
    // a1 now has 4 elements: 3 numbers and 1 array
    

  • Moderators

    @JKSH said in C++ % " ?:

    Wait till you try + or += on QJsonArray ;)

    QJsonArray a1{1, 2, 3};
    QJsonArray a2{4, 5, 6};
    
    a1 += a2;
    // a1 now has 4 elements: 3 numbers and 1 array
    

    😑😔
    of course it does.

    Not a fan!


Log in to reply