Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy List Initialization? Why does this compile?

Tags:

c++

I am using Microsoft Visual Studio Community 2019, V16.5.2. I want to test list initialization

Please see the following test program:

#include <string>

void foo(std::string str) {}

int main() {

    foo( {"str1", "str2"} );

    return 0;
}

This compiles without error and warning. Why?

It gives a runtime error: Expression: Transposed pointer range

Can somebody please explain what is happening here?


Edit.

I dissasembled the code and run it in the debugger

    foo( {"str1", "str2"} );
00F739A8  sub         esp,1Ch  
00F739AB  mov         esi,esp  
00F739AD  mov         dword ptr [ebp-0C8h],esp  
00F739B3  lea         ecx,[ebp-0D1h]  
00F739B9  call        std::allocator<char>::allocator<char> (0F7136Bh)  
00F739BE  push        eax  
00F739BF  push        offset string "str2" (0F84DB8h)  
00F739C4  push        offset string "str1" (0F84E2Ch)  
00F739C9  mov         ecx,esi  
00F739CB  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> ><char const *,0> (0F71569h)  
00F739D0  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > (0F71843h)  
00F739D5  add         esp,1Ch  

It crashes at the first call to the constructor?

like image 353
Armin Montigny Avatar asked Apr 03 '20 21:04

Armin Montigny


People also ask

What is copy list initialization?

In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. [ Note: This differs from other situations (13.3.1.3, 13.3.1.4), where only converting constructors are considered for copy initialization.

How do you initialize a list in C++?

When do we use Initializer List in C++? Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.


1 Answers

std::string has a template constructor that builds a string from a begin/end iterator pair. String literals in C++ devolve down to const char*s. And pointers are iterators. Therefore, list initialization picked the begin/end pair constructor.

You got a runtime error because the two pointers do not actually create a valid range, which cannot be determined at compile-time (generally).

like image 117
Nicol Bolas Avatar answered Sep 25 '22 14:09

Nicol Bolas