Itachi Uchiwa
Itachi Uchiwa

Reputation: 3164

Is copying a range from a container to itself safe or yields runtime error?

Why in C++ primer is said that we cannot insert to a container a range denoted by iterators to the same range (of the container itself)?

std::list<string> slist{"hi", "there!"};
slist.insert(slist.cbegin(), slist.cbegin(), slist.cend());

for(auto const& s : slist)
    std::cout << s << ' ';
std::cout << '\n';

The output:

hi there! hi there!

Upvotes: 0

Views: 74

Answers (0)

Related Questions