ArduinoCore-API icon indicating copy to clipboard operation
ArduinoCore-API copied to clipboard

String relational operators are members.

Open BillyDonahue opened this issue 7 years ago • 0 comments

Member relational operators are no good because they cause the lhs and rhs to be treated differently. This causes problems such as making == appear non-commutative. Such as:

void setup() {
  String("hi") == "world";
  "world" == String("hi");
}
sketch_jan19a:3: error: no match for 'operator==' (operand types are 'const char [6]' and 'String')
   "world" == String("hi");
           ^
exit status 1

It would be less user-surprising to bring these operators out of the class.

I propose defining them at namespace scope as symmetric pairs of operators, between a String on one side, and everything String accepts as an implicit conversion on the other side, per: http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

BillyDonahue avatar Jan 19 '18 07:01 BillyDonahue