4 ms·
It's matching std::vector's implicit constructor which takes two iterators, which can be initialized with any pointer. Note that it won't compile with one, thre
by mwkaufma 2y ago
It's matching std::vector's implicit constructor which takes two iterators, which can be initialized with any pointer. Note that it won't compile with one, three, or more string-literal char*s, but exactly two.
- metadat 2y agoWhy only exactly two? The whole "UB but it still compiles" thing, is pretty gross.
- comex 2y agoBecause the constructor takes two arguments, a "begin" iterator and an "end" iterator. And pointers count as valid iterators. Normally you wouldn't use the "= {}" syntax to invoke a constructor this way. Instead of `std::vector<int> v = {begin, end}` you would usually write `std::vector<int> v(begin, end)`. But for some reason C++11 decided to make those two things mostly equivalent.
- UncleMeat 2y agoThis sort of UB detection requires bespoke work. The compiler probably can figure out that the begin and end iterators here are referencing different objects but if you add just a bit more complexity to the code then the compiler won't be able to prove that.
- mwkaufma 2y agoThat's consistent with STL's move to use "explicit" and other narrowing annotations, as described in the article.