简体   繁体   中英

Overload operator for different template parameters

#include <cstddef>

template <typename T, std::size_t MaxElements>
struct circular_buffer{};

template <typename circular_buffer>
bool operator==(const circular_buffer &a, const circular_buffer &b) {
    return true;
}

int main() {
    circular_buffer<int, 2> a;
    circular_buffer<int, 3> b;
    a == b;
    return 0;
}

This code is work only for 2==3. But my 2 != my 3. Help me to repair operator.

Following might work for you:

template <typename T, std::size_t M1, std::size_t M2>
bool operator==(circular_buffer<T, M1> const & a, circular_buffer<T, M2> const & b) {
    // some meaningful logic
    return true;
};

Problem you have is that a and b are of different types, ie those two objects have different MaxElements template parameter. Thus, you need to provide operator == overload that will handle this case.

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