5 ms·
> suppose you have a std::vector<std::vector<int>> and you want to append a number to the end of the first vector, but you accidentally forget the subscript. ..
by vintagedave 2y ago
> suppose you have a std::vector<std::vector<int>> and you want to append a number to the end of the first vector, but you accidentally forget the subscript. ... if you write my_vec.emplace_back(2<<20), the code will compile, and you won’t notice any problems until run-time.
How is this not caught by the type system? 2<<20 is clearly no way, whatsoever, compatible with a vector<int> type.
- dullcrisp 2y agoIiuc, it’ll call the vector<int> constructor with 2<<20 and make you a huge vector.
- deleted 2y ago[deleted]
- wrsh07 2y agoThe compiler can't read your intention. Emplace means "forward these arguments to the constructor", so it forwards those arguments to the constructor (in this case, vector<int>). With push back it would be caught by the compiler because you can't implicitly convert 2<<20 to a vector<int>