Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior of std::initializer_list of std::strings

This question is already asked most likely, but I did not find the answer.

The code below compiles with gcc but crashes at runtime, with std::length_error (live).

void test(const std::string &value) { std::cout << "string overload: " << value << std::endl; }

//void test(const std::vector<std::string> &) { std::cout << "vector overload" << std::endl; }

int main()
{
    test({"one", "two"});
}

The ability to create a string from the initializer list of strings seems controversial and, for example, does not make it possible to create the overload commented out in the code above.

But even if such construction is allowed, why does it lead to a failure?

like image 217
Yuriy Avatar asked Aug 30 '19 10:08

Yuriy


2 Answers

It calls

string(const char* b, const char* e) 

string ctor overload.

It works only if b and e points to the same string literal. Otherwise it is undefined behaviour.

like image 66
rafix07 Avatar answered Nov 11 '22 09:11

rafix07


For starters there is no used the constructor that accepts an initializer list because such a constructor looks like

basic_string(initializer_list<charT>, const Allocator& = Allocator());
                              ^^^^^

So the compiler searches another appropriate constructor and it finds such a constructor. It is the constructor

template<class InputIterator>
basic_string(InputIterator begin, InputIterator end, const Allocator& a = Allocator());

That is the expressions "one" and "two" are considered as iterators of the type const char *.

So the function test has undefined behavior.

You could write for example (provided that string literals with the same content are stored as one string literal in memory, which is not guaranteed and depends on the selected compiler options).

#include <iostream>
#include <string>

void test(const std::string &value) { std::cout << "string overload: " << value << std::endl; }

//void test(const std::vector<std::string> &) { std::cout << "vector overload" << std::endl; }

int main()
{
    test({ "one", "one" + 3 });
}

And you will get a valid result.

string overload: one

Pay attention to that this construction

{ "one", "two" }

is not an object of the type std::initializer_list<T>. This construction does not have a type. It is a braced-init-list that is used as an initialzer. Simply the compiler tries at first to use a constructor that have the first parameter of the type std::initializer_list to use with this initializer.

For example if you will use the class std::vector<const char *> then indeed the compiler will use its constructor with std::initializer_list and correspondingly initializes its parameter with this braced-init-list. For example

#include <iostream>
#include <vector>

int main()
{
    std::vector<const char *> v( { "one", "two" } );

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << '\n';
}
like image 37
Vlad from Moscow Avatar answered Nov 11 '22 10:11

Vlad from Moscow