I wanted to see at how C++ vector is made. I found this, Implementation is LLVM compiler https://llvm.org/svn/llvm-project/libcxx/trunk/src/vector.cpp appleclang
src/vector.cpp:
#include "vector"
_LIBCPP_BEGIN_NAMESPACE_STD
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __vector_base_common<true>;
_LIBCPP_END_NAMESPACE_STD
Implementation https://llvm.org/svn/llvm-project/libcxx/trunk/include/vector appleclang LLVM.
include/vector:
// .. deleted code
template <bool>
class __vector_base_common
{
protected:
_LIBCPP_ALWAYS_INLINE __vector_base_common() {}
_LIBCPP_NORETURN void __throw_length_error() const;
_LIBCPP_NORETURN void __throw_out_of_range() const;
};
template <bool __b>
void
__vector_base_common<__b>::__throw_length_error() const
{
_VSTD::__throw_length_error("vector");
}
template <bool __b>
void
__vector_base_common<__b>::__throw_out_of_range() const
{
_VSTD::__throw_out_of_range("vector");
}
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>)
// .. deleted code
template <class _Tp, class _Allocator>
class __vector_base
: protected __vector_base_common<true>
// .. deleted code
class _LIBCPP_TEMPLATE_VIS vector
: private __vector_base<_Tp, _Allocator>
// .. deleted code
I have many questions how a vector is made.. it feel embarrassing to ask even one.. but.. why is __vector_base_common taking bool template parameter? It doesn't seem to use it, and I verified only use in code is __vector_base_common<true>, the false value is not used.
Edit: There have been multiple suggestion relating vector<bool> to this. Only one specialisation of the above bool parameter is used (true). This is what vector special looks like
template <class _Allocator>
class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator>
: private __vector_base_common<true>
The difference looks between private versus protected... Is this a space optimisation for vector not needing those throwing member functions? Still I have question why __vector_base_common needs a template parameter. Is this C++ pattern with a name?
__vector_base_common<true>or__vector_base_common<false>is explicitly specialized. It may have something to do withstd::vector<bool>being a special case, where it would inherit from one__vector_base_commonwhere as the general case would inherit from the other. - François Andrieux