Search code examples
default-comparisons

C++20 Keep typedef struct for operator==


C++20 provides very simple way to compare struct, example I want to typedef my struct in header file this way

typedef struct tfecha {
    int a;
    bool operator==(const tfecha_t&) const = default;
}tfecha_t;

But error: ‘tfecha_t’ does not name a type
error: defaulted member ‘bool tfecha::operator==(const int&) const’ must have parameter type ‘const tfecha&’


Solution

  • Err happen because of typo, use not const tfecha_t& but const tfecha& in line

    bool operator==(const tfecha&) const = default;
    

    Full example online

    #include <iostream>
    typedef struct tfecha {
        int a;
        bool operator==(const tfecha&) const = default;
    }tfecha_t;
    
    int main(){
        tfecha_t s1= {.a= 12};
        tfecha_t s2= {.a= 12};
        bool f= s1==s2;
        std::cout << f;
        return 0;
    }