Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68505
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Initialization of std::vector<std::string> throws |
| Date | 2019-09-18 08:37 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86blvh4nus.fsf@linuxsc.com> (permalink) |
| References | <qko9ig$97d$1@dont-email.me> <cone.1567599651.572642.18978.1004@monster.email-scan.com> <qkoav3$97d$2@dont-email.me> <RlkcF.175481$Pr1.15721@fx01.iad> |
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> writes:
> Ralf Goertz wrote:
>
>> Am Wed, 04 Sep 2019 08:20:51 -0400
>> schrieb Sam <sam@email-scan.com>:
>>
>>> Ralf Goertz writes:
>>>
>>>> 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
>
> my guess is the above pt declarator is roughly equivalent to
> pt(vector<int>(initializer_list<int>(initializer_list<int>(1,2)))) --
> which just happens to give you an expected result. I think it is
> undefined or implementation-specific what default copy constructor does
> on initializer_list because the data members of it are not defined by
> the standard.
This seems wrong to me. Please note I am not saying it IS wrong,
only that it seems to me to be wrong. More specifically I don't
think undefined behavior plays a role. I tried reading the
Standard to verify that but found it impenetrable in this area.
Can someone help find the relevant passages (and an accompanying
explanation would also be appreciated)?
>> 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?
>
> for vector<int>, because there is constructor with count and value, you
> need something like = { 1, 2}; (note =) for {1,2} to be interpreted as
> initializer_list; for vector<string> you should be able to initialize
> with {"One", "Two"} directly.
I'm pretty sure what you say about vector<int> isn't right. A
declaration like
vector<int> foo { 10, 1 };
gives a vector of length 2, not a vector of length 10. If the
braces were parentheses
vector<int> bas ( 10, 1 );
we would get a vector of length 10, using the ( count, value )
constructor.
Disclaimer: the comments above are based solely on experimental
results, not on understanding what the Standard says about it.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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