2 ms·
Why only exactly two? The whole "UB but it still compiles" thing, is pretty gross.
by metadat 2y ago
Why 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.