Many STL containers have a range constructor that you can use to initialize the container elements:
char ary[] = {'a', 'b', 'c'};
vector<char> v(ary, ary + 3);
I subclass specific STL containers, solely for the purpose of using a custom allocator by default:
template<class _Ty, class _Ax = stl_allocator<_Ty> >
class xvector
: public std::vector<_Ty, _Ax>
{
};
This works well, but I can't use the range constructor anymore:
xvector<char> v2(ary, ary + 3);
Generates the following error:
error C2661: 'xvector<_Ty>::xvector' : no overloaded function takes 2 arguments
with
[
_Ty=char
]
However, I can use the assign method:
xvector<char> v3;
v3.assign(ary, ary + 3);
Can someone explain why the range-constructor doesn't work while the assign method does? I'm using VC++ 2008 and from what I can tell the argument list of the range constructor and the assign method is exactly the same.
Update Thank you for your answers, but for me the key-point is that I forgot that constructurs aren't inherited and so only the default constructor is available (it doesn't have anything to do with range-constructors).
I also should have mentioned that I'm forced to stick to VS2008 / C++ 03 because the target platform doesn't support more recent compilers, unfortunately.