← Top stories In C++, = default and {} are not the same constructor ↗ (opens external site) 129 points by surprisetalk 2 days ago | lemire.me | 198 comments on HN ↗ View full thread · View on Hacker News Comment thread (1) PhilipRoman | 2 days ago | parent | reply I'm no C++ expert, but in P2012R0 it even gives this specific example, indicating that A is OK in c++20:[..] One recent real-world example by one of the authors is the following: Improving the following code: struct Person { std::vector<int> values; … }; for (auto elem : createPerson().values) { // OK (lifetime extended) <----------- by introducing a getter to have better encapsulation, suddenly caused the undefined behavior: class Person { private: std::vector<int> values; … public: const auto& getValues() const { return values; } }; for (auto elem : createPerson().getValues()) { // UB (fatal runtime error)
[..] One recent real-world example by one of the authors is the following: Improving the following code:
by introducing a getter to have better encapsulation, suddenly caused the undefined behavior: