Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68505
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: Initialization of std::vector<std::string> throws |
| Date | Wed, 18 Sep 2019 08:37:31 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 92 |
| 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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="d7421874ff1696044f351dbc5e408957"; logging-data="28854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pdWFZOUMHmZflBV6StWhGd51oNivEaxg=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:FwWxIoyB0MJTSX2naRnvJpnFqQg= sha1:0wwgLBzfmK9W0EWu8J94G8VuIzU= |
| Xref | csiph.com comp.lang.c++:68505 |
Show key headers only | View raw
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