简体   繁体   中英

Defining two-argument operator overload for private struct

I'm trying to overload an operator for containers of a private struct to be used only within the class (comparing std::deque<T> against std::vector<T> with operator==() ).

I am used to declaring operator overloads as shown below.

This prints "1" as I would expect, but the struct is public.

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>

class Example1 {
public:
    struct S1 {
        bool flag;
        unsigned value;

        S1(const unsigned value) :
            flag(false), value(value)
        {
            // Empty
        }

        bool operator==(const S1 &rhs) const {
            return flag == rhs.flag && value == rhs.value;
        }
    };

    static void test();
};

inline bool operator==(const std::deque<Example1::S1> &d, const std::vector<Example1::S1> &v) {
    return d.size() == v.size() && std::equal(d.begin(), d.end(), v.begin());
}

inline bool operator==(const std::vector<Example1::S1> &v, const std::deque<Example1::S1> &d) {
    return d == v;
}

void Example1::test() {
    std::vector<S1> v1 { 1, 2, 3, 4 };
    std::deque<S1> d1 { 1, 2, 3, 4 };

    std::cout << (v1 == d1) << "\n";
}

int main() {
    Example1::test();
}

How should I define these operators when the struct is private?

I tried the following.

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>

class Example2 {
public:
    static void test();
private:
    struct S2 {
        bool flag;
        unsigned value;

        S2(const unsigned value) :
            flag(false), value(value)
        {
            // Empty
        }

        bool operator==(const S2 &rhs) const {
            return flag == rhs.flag && value == rhs.value;
        }
    };

    bool operator==(const std::deque<S2> &d, const std::vector<S2> &v) const {
        return d.size() == v.size() && std::equal(d.begin(), d.end(), v.begin());
    }

    bool operator==(const std::vector<S2> &v, const std::deque<S2> &d) const {
        return d == v;
    }
};

void Example2::test() {
    std::vector<S2> v1 { 1, 2, 3, 4 };
    std::deque<S2> d1 { 1, 2, 3, 4 };

    std::cout << (v1 == d1) << "\n";
}

int main() {
    Example2::test();
}

But the overloads can only have one argument:

main.cpp:25:72: error: ‘bool Example2::operator==(const std::deque&, const std::vector&) const’ must take exactly one argument
     bool operator==(const std::deque<S2> &d, const std::vector<S2> &v) const {

Your current attempt tries to overload Example2::operator== , which you can't do with two arguments.

The simple solution is to define those operator functions as friends of the Example2 class:

friend bool operator==(const std::deque<S2> &d, const std::vector<S2> &v) {
    return d.size() == v.size() && std::equal(d.begin(), d.end(), v.begin());
}

friend bool operator==(const std::vector<S2> &v, const std::deque<S2> &d) {
    return d == v;
}

This will define the functions as non-member functions.

You might use friendship:

friend bool operator==(const std::deque<S2> &d, const std::vector<S2> &v) {
    return d.size() == v.size() && std::equal(d.begin(), d.end(), v.begin());
}

friend bool operator==(const std::vector<S2> &v, const std::deque<S2> &d) {
    return d == v;
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM