Search code examples
c++operator-overloadingequals-operator

C++: call original definition of operator equals


I am overloading the operator equals (==) as show bellow:

#include <string>
#include <algorithm>

bool operator == (std::string str1, std::string str2) {
    std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
    std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
    return (str1 == str2);
}

but, the problem appear on line return (str1 == str2), because operator == is called recursively. So, how can I call the original definition for operator equals (not the overloaded) ?

Best regards


Solution

  • return std::operator==(str1, str2);
    

    or

    return (str1.compare(str2) == 0);
    

    Though I suspect you'll have ambiguous overload error, between your operator== found by ordinary lookup, and std::operator== found by argument-dependent lookup. In any case, attempting to change the meaning of str1 == str2 may not be the best idea; if nothing else, it violates the Principle of Least Astonishment.