1

I'm trying to overload binary operator+ for nested template class with clang and got the error invalid operands to binary expression. Note: candidate template ignored: couldn't infer template argument T. Something like:

template<typename T>
class Container
{
  struct Iterator {
    template<typename U>
    friend typename Container<U>::Iterator operator+(size_t, typename Container<U>::Iterator const&);
   };
};

template<typename U>
typename Container<U>::Iterator
operator+(size_t, typename Container<U>::Iterator const&)
{
  return Container<U>::Iterator{};
}

Is there way to overload binary operators for nested template class using clang compiler?

C++17 can be used.

2
  • The left side of :: is an undeduced context. It's not possible to deduce U. Commented Jan 19, 2018 at 7:57
  • Also, it is far easier to do these sorts of templates inline within the nested class declaration, otherwise you end up needing to do template<typename T> template<typename U> Commented Jan 19, 2018 at 7:59

1 Answer 1

2

Don't make the inner function a template. Just make it a non-template non-member friend and define it inline:

template<typename T>
class Container
{
  struct Iterator {
    friend Iterator operator+(size_t, Iterator const&) {
      return {};
    } 
  };
};

This works in C++11. C++03 if you don't {} in the return.

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.