Given a type C
which is an STL-conforming container, how do I correctly detect if C
contains a member function reserve
? I tried the following approach (with GCC 4.6.3):
template< typename C, typename = void >
struct has_reserve
: std::false_type
{};
template< typename C >
struct has_reserve< C, typename std::enable_if<
std::is_same<
decltype( &C::reserve ),
void (C::*)( typename C::size_type )
>::value
>::type >
: std::true_type
{};
This works for C
being std::vector
, but not for the unordered containers, e.g. std::unordered_set
. The reason is, that reserve
is a (direct) member function of std::vector
, but for the unordered containers it is inherited from a base class, i.e., its signature is not void (C::*)( typename C::size_type )
but void (B::*)( typename C::size_type )
for some unspecified base class B
of C
.
I know how to work around it and detect reserve
even if inherited, but it looks clumsy and I wonder what is allowed by the standard. So...
My question is: Does the standard allow reserve
to be inherited from an unspecified base class or is the synopsis binding and requires a direct member function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…