Re: What is wrong with this?
On Feb 13, 3:52 pm, "mlt" <a...@asd.com> wrote:
This seems to solve the problem:
std::vector<std:.vector<int>> vec(1)(1);
Are you sure it wasn't:
std::vector<std:.vector<int> > vec(1, 1) ;
The line you've posted shouldn't compile (and I can't imagine
what it should mean if it did).
It's also worth noting that there's a little bit of uncertainty
about the legality of the line I just wrote. Logically, it
should be:
std::vector< std::vector< int > > vec( 1,
std::vector< int >( 1 ) ) ;
There is special wording in the standard, however, to make
things like:
std::vector< int > vec( 10, 5 ) ;
work---the constructor that gets chosen here is an instantiation
of:
template< typename InputIterator >
vector< int >::vector( InputIterator begin, InputIterator end ) ;
while the one we (obviously want) is:
vector< int >::vector( size_t n, int init ) ;
(This second one requires a conversion of int to size_t, where
as the instantiation of the first is an exact match.) So the
standard has some special wording to make this constructor do
the right thing. Whether intended or not, it also makes
std::vector<std:.vector<int> > vec(1, 1) ;
work. (There was a defect report about this, but I don't know
what the final decision was.)
All of which probably isn't that important (unless the final
decision was to render my first suggestion illegal). What's
important is to realize that this idiom doesn't extend. You
can't write:
std::vector< std::vector< std::vector< int > > >
vec( 1, 1, 1 ) ;
You have to do it right:
std::vector< std::vector< std::vector< int > > >
vec(
1, std::vector< std::vector< int > >(
1, std::vector< int >( 1 ) ) ) ;
(Obviously, a few typedefs will make the declaration a lot
simpler.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34