Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 It doesn't return 2 values. It returns 3 values, a bit like strcmp. As usual, Cppreference is here to explain though also as usual it's C++20 and cppreference is still catching up. What I didn't know until you asked is that just defining that gives you all the comparison operators; maybe this time next year everyone will support it and I can use it in things. URL: https://forum.audiogames.net/post/570498/#p570498 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @2, right, but again, what is the point of such an operator? Where could I practically use it? Like I said, logic suggests that the _expression_ a <=> b will always be true. URL: https://forum.audiogames.net/post/570505/#p570505 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @3It returns 3 values, not 2. Read the linked page. It's a different value if a < b, a==b, or a > b. You define it in your class to return 1 of 3 values from an enum and then the rest of the comparison operators can be implemented by the compiler on your behalf, rather than you having to hit all 5 functions yourself.Whether all the values it returns are truthy, I don't know, but it's not returning a bool. URL: https://forum.audiogames.net/post/570514/#p570514 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @4, oh, okay. Thanks. URL: https://forum.audiogames.net/post/570518/#p570518 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 Hello,Usually, three way comparison returns -1, 0 or 1 depending on if left operand is less, equal or greather than right operand.In fact, it's a generalization of strcmp. It isn't very useful in if/else statements as such, but is very useful in find, search and sort algorithms.Usually one implement std::less a.k.a. operator <, and all other comparisons are made from it, i.e. a>b is b=b is !(aIn sorted datastructures, three way comparison is known to be simpler to implement and a little more efficient than std::less for a various set of cases. String comparison is one of them.For your small example, given that it generally returns -1, 0 or 1, I think that x<=>y will return the same as x!=y. URL: https://forum.audiogames.net/post/570843/#p570843 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 Hello,Usually, three way comparison returns -1, 0 or 1 depending on if left operand is less, equal or greather than right operand.In fact, it's a generalization of strcmp. It isn't very useful in if/else statements as such, but is very useful in find, search and sort algorithms.Usually one implement std::less a.k.a. operator <, and all other comparisons are made from it, i.e. a>b is b=b is !(aIn sorted datastructures, three way comparison is known to be simpler to implement and a little more efficient than std::less for a various set of cases. String comparison is one of them.For your small example, given that it generally returns -1, 0 or 1, I think that x<=>y will return the same as x!=y.In C++, comparisons is based on std::less for historical reasons. Many more recent languages always use three way comparison as a default, for example Java with Comparator and Comparable interfaces. URL: https://forum.audiogames.net/post/570843/#p570843 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 I don't think <=> always returns -1, 0, 1. That's certainly not what cppreference is implying. URL: https://forum.audiogames.net/post/570942/#p570942 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @7, agreed; the proposed wording doesn't imply that either. From the proposal: "If one of the operands is of type bool and the other is not, the program is ill-formed. If both operands have arithmetic types, the usual arithmetic conversions are applied to the operands. Then: If a narrowing conversion is required, other than from an integral type to a floating point type, the program is ill-formed. Otherwise, if the operands have integral type, the operator yields a prvalue of type std::strong_ordering. The result is std::strong_ordering::equal if both operands are arithmetically equal, std::strong_ordering::less if the first operand is arithmetically less than the second operand, and std::strong_ordering::greater otherwise. Otherwise, the operands have floating-point type, and the operator yields a prvalue of type std::partial_ordering. The _expression_ a <=> b yields std::partial_ordering::less if a is less than b, std::partial_ordering::greater if a is greater than b, std::partial_ordering::equivalent if a is equivalent to b, and std::partial_ordering::unordered otherwise. If both operands have the same enumeration type E, the operator yields the result of converting the operands to the underlying type of E and applying <=> to the converted operands. If at least one of the operands is a pointer, array-to-pointer conversions (7.2 [conv.array]), pointer conversions (7.11 [conv.ptr]), function pointer conversions (7.13 [conv.fctptr]), and qualification conversions (7.5 [conv.qual]) are performed on both operands to bring them to their composite pointer type (Clause 8 [expr]). If at least one of the operands is a pointer to member, pointer to member conversions (7.12) and qualification conversions (7.5) are performed on both operands to bring them to their composite pointer type (Clause 8). If both operands are null pointer constants, but not both of integer type, pointer conversions (7.11 [conv.ptr]) are performed on both operands to bring them to their composite pointer type (Clause 8 [expr]). In all cases, after the conversions, the operands shall have the same type. [ Note: If both of the operands are arrays, array-to-pointer conversions are not applied. – end note ] If the composite pointer type is a function pointer type, a pointer-to-member type, or std::nullptr_t, the operator yields a prvalue of type std::strong_equality; the operator yields std::strong_equality::equal if the (possibly converted) operands compare equal (8.10 [expr.eq]) and std::strong_equality::unequal if they compare unequal, otherwise the result of the operator is unspecified. If the composite pointer type is an object pointer type, p <=> q returns a strong_ordering. If two pointer operands p and q compare equal (8.10 [expr.eq]), p <=> q yields std::strong_ordering::equal; if p and q compare unequal, p <=> q yields std::strong_ordering::less if q compares greater than p and std::strong_ordering::greater if p compares greater than q (8.9 [expr.rel]). Otherwise, the result is unspecified. Otherwise, the program is ill-formed." URL: https://forum.audiogames.net/post/571015/#p571015 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @7, agreed; the proposed wording doesn't imply that either. From the proposal: "If one of the operands is of type bool and the other is not, the program is ill-formed. If both operands have arithmetic types, the usual arithmetic conversions are applied to the operands. Then: If a narrowing conversion is required, other than from an integral type to a floating point type, the program is ill-formed. Otherwise, if the operands have integral type, the operator yields a prvalue of type std::strong_ordering. The result is std::strong_ordering::equal if both operands are arithmetically equal, std::strong_ordering::less if the first operand is arithmetically less than the second operand, and std::strong_ordering::greater otherwise. Otherwise, the operands have floating-point type, and the operator yields a prvalue of type std::partial_ordering. The _expression_ a <=> b yields std::partial_ordering::less if a is less than b, std::partial_ordering::greater if a is greater than b, std::partial_ordering::equivalent if a is equivalent to b, and std::partial_ordering::unordered otherwise. If both operands have the same enumeration type E, the operator yields the result of converting the operands to the underlying type of E and applying <=> to the converted operands. If at least one of the operands is a pointer, array-to-pointer conversions (7.2 [conv.array]), pointer conversions (7.11 [conv.ptr]), function pointer conversions (7.13 [conv.fctptr]), and qualification conversions (7.5 [conv.qual]) are performed on both operands to bring them to their composite pointer type (Clause 8 [expr]). If at least one of the operands is a pointer to member, pointer to member conversions (7.12) and qualification conversions (7.5) are performed on both operands to bring them to their composite pointer type (Clause 8). If both operands are null pointer constants, but not both of integer type, pointer conversions (7.11 [conv.ptr]) are performed on both operands to bring them to their composite pointer type (Clause 8 [expr]). In all cases, after the conversions, the operands shall have the same type. [ Note: If both of the operands are arrays, array-to-pointer conversions are not applied. – end note ] If the composite pointer type is a function pointer type, a pointer-to-member type, or std::nullptr_t, the operator yields a prvalue of type std::strong_equality; the operator yields std::strong_equality::equal if the (possibly converted) operands compare equal (8.10 [expr.eq]) and std::strong_equality::unequal if they compare unequal, otherwise the result of the operator is unspecified. If the composite pointer type is an object pointer type, p <=> q returns a strong_ordering. If two pointer operands p and q compare equal (8.10 [expr.eq]), p <=> q yields std::strong_ordering::equal; if p and q compare unequal, p <=> q yields std::strong_ordering::less if q compares greater than p and std::strong_ordering::greater if p compares greater than q (8.9 [expr.rel]). Otherwise, the result is unspecified. Otherwise, the program is ill-formed."I really, really wish that all these things that are "unspecified" would result in more "ill-formed" programs, rather than UB. I thought the hole point of C++17/20 was to make the language safer, no? URL: https://forum.audiogames.net/post/571015/#p571015 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 Where are you getting a reasonably accessible copy of the C++ standard that's not LaTeX that you can copy/paste from?Imo unspecified is about "if we specify this the standard will start requiring a bookshelf instead of just a huge book". There's enough C++ cruft that you can't ever fully specify everything at this point. URL: https://forum.audiogames.net/post/571029/#p571029 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @9, its directly taken from the paper, sec. 4. And if you want a copy of the standard that's not LaTex, you can always buy it from ANSI. URL: https://forum.audiogames.net/post/571070/#p571070 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @10As an accessible PDF? I don't necessarily mind buying it, but it's a *ton* of typeset math, and every other rendered version of this stuff that I've seen has had issues of one sort or another. URL: https://forum.audiogames.net/post/571071/#p571071 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @11, yeah, there's a ton of math, especially in standards like the C one (section 5.2 comes to mind). But though that math doesn't render right, most of it does, including the important bits like the syntax sections. I use QRead for my PDFs -- it does quite well, even though I wish I had a better alternative solution. URL: https://forum.audiogames.net/post/571092/#p571092 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 I don't think <=> always returns -1, 0, 1. That's certainly not what cppreference is implying.Exact, it's three special values of a special type. But I wanted to explain the general principle simpler and where it comes from.There's no need to copy the C++ spec. URL: https://forum.audiogames.net/post/572176/#p572176 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @13Both me and Ethin are at the level of C++ knowledge where copying the C++ spec is the easiest way to have the discussion. URL: https://forum.audiogames.net/post/572184/#p572184 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 @13, what 14 said. To add to that though, I don't think <=> is designed to be used in a conditional statement at all -- at least, not like if (a<=>b). Rather, the spec says that p<=>q returns std::strong_ordering/partial_ordering/..., so I think your supposed to assign its result, then do something like a conditional or a switch on the result of the _expression_. URL: https://forum.audiogames.net/post/572243/#p572243 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
Re: Confusion on the spaceship operator in C++20
Re: Confusion on the spaceship operator in C++20 I'm almost sure that 99.9% of us are just supposed to use it as a fast way to get all of the comparison methods with 4 lines of code. URL: https://forum.audiogames.net/post/572246/#p572246 -- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector