For some necessary reasons, I have subclassed some standard containers like std::vector
with private
inheritance.
template<typename T>
struct MyVector : private std::vector<T>
{
typedef std::vector<T> vector;
using vector::begin; // ok
using vector::end; // ok
using vector::size; // ok
using vector::operator ==; // <---- error
// Other customized methods
};
Most of the times, the using
syntax is used to bring those methods into the scope of the subclass.
The explicit overload is done, only if I want to do something extra.
Everything is working fine except the std::vector::operator ==
is not coming to the scope and gives compiler error:
error: no members matching
MyVector<int>::vector
{akastd::vector<int, std::allocator<int> >}::operator==
inMyVector<int>::vector {aka class std::vector<int, std::allocator<int>
I tried to simulate the situation with custom classes and it compiles fine.
With the standard container, the compilation fails.
From browsing the source of std::vector
, I get to see operator ==
present inside the class body.
What is the correct syntax to bring the operator ==
into the scope of MyVector
?
Most of the operator overloads in the standard containers are non-members so you wouldn't be able to find them.
I'll do note however that this (from what I've read in the standard) isn't required to be the case. However it seems like most common implementations will do this. If you want to use std::vector
's operator==
you can overload it like it is defined in the standard using with what you have brought in you can do it like so:
bool operator == (const MyVector &other) const
{
const std::vector &this_ = *this, &other_ = other;
return this_ == other_;
}