Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #68211

Re: Initialization of std::vector<std::string> throws

From Ralf Goertz <me@myprovider.invalid>
Newsgroups comp.lang.c++
Subject Re: Initialization of std::vector<std::string> throws
Date 2019-09-04 14:33 +0200
Organization A noiseless patient Spider
Message-ID <qkoav3$97d$2@dont-email.me> (permalink)
References <qko9ig$97d$1@dont-email.me> <cone.1567599651.572642.18978.1004@monster.email-scan.com>

Show all headers | View raw


Am Wed, 04 Sep 2019 08:20:51 -0400
schrieb Sam <sam@email-scan.com>:

> Ralf Goertz writes:
> 
> > Hi,
> >
> > why does the declaration
> >
> > std::vector<std::string> pt({{"One","Two"}});
> >
> > throw:
> >
> > terminate called after throwing an instance of 'std::length_error'
> >   what():  basic_string::_M_create  
> 
> Because this is undefined behavior.
> 
> > whereas neither
> >
> > std::vector<std::string> pt({{"One"}});
> >
> > nor
> >
> > std::vector<int>  pt({{1,2}});  
> 
> If you actually examine the end result the last declaration, you will
> discover, to your surprise, a std::vector<int> with just one value,
> and that would hopefully be a big honking clue as to what this is
> doing.

Hm, I did that and it failed to surprise me.


#include <vector>
#include <string>
#include <iostream>

std::vector<int>  pt({{1,2}});
int main() {
    for (auto p:pt) std::cout<<p<<std::endl;
}

gives: 
1
2

here. Just as I expected.
> { /* something */ } introduces a braced initialization list.
> 
> { { /* something */ } } introduces a braced initialization list with
> one value.

But didn't we need to use two braces for container intialization?

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Initialization of std::vector<std::string> throws Ralf Goertz <me@myprovider.invalid> - 2019-09-04 14:09 +0200
  Re: Initialization of std::vector<std::string> throws Sam <sam@email-scan.com> - 2019-09-04 08:20 -0400
    Re: Initialization of std::vector<std::string> throws Ralf Goertz <me@myprovider.invalid> - 2019-09-04 14:33 +0200
      Re: Initialization of std::vector<std::string> throws Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2019-09-05 23:26 -0400
        Re: Initialization of std::vector<std::string> throws Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-18 08:37 -0700
      Re: Initialization of std::vector<std::string> throws Ralf Goertz <me@myprovider.invalid> - 2019-11-22 12:12 +0100
        Re: Initialization of std::vector<std::string> throws Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-11-22 13:44 +0000
          Re: Initialization of std::vector<std::string> throws Ralf Goertz <me@myprovider.invalid> - 2019-11-22 16:03 +0100
            Re: Initialization of std::vector<std::string> throws Bo Persson <bo@bo-persson.se> - 2019-11-22 16:22 +0100
            Re: Initialization of std::vector<std::string> throws Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-11-22 16:13 +0000
    Re: Initialization of std::vector<std::string> throws "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-11-22 16:30 +0100
  Re: Initialization of std::vector<std::string> throws Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-04 14:43 +0200

csiph-web