0
#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.

4
  • @NutCracker No, I do not use != neverwhere. Commented Nov 5, 2020 at 7:16
  • circular_buffer shouldn't be a template parameter to your operator. That operator is worthless as is. Beyond that, do you understand that by specifying two different sizes you actually have two different end concrete types? One is circular_buffer<int,2>, the other is circular_buffer<int,3> . They're not the same type. What is it you hope to achieve with your operator == when that happens (or for that matter, in general)? Commented Nov 5, 2020 at 7:19
  • @WhozCraig But what parametr must be instead this? Commented Nov 5, 2020 at 7:20
  • template <typename circular_buffer> name is arbitrary, and currently just hide (for the function) the template class with the same name. So it is misleading. Commented Nov 5, 2020 at 7:38

1 Answer 1

2

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, i.e. those two objects have different MaxElements template parameter. Thus, you need to provide operator == overload that will handle this case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.