I tested the following code, but the results of gcc/clang and MSVC seem different.
Is this undefined behavior or an MSVC bug? I thought assigning a value to a C++ std container is a deep copy, according to SO answers such as this and this. So I thought this is a legal code.
Please help me if I missed some basic concepts of C++.
#include <iostream>
#include <vector>
#include <map>
#include <tuple>
struct S {
std::vector<int> v;
std::tuple<std::vector<int>&> tup{v};
};
int main() {
std::map<int, std::map<int, S>> m_n_s;
for (int j = 0; j < 1; ++j) {
std::map<int, S> n_s;
for (int i = 0; i < 1; ++i) {
std::vector<int> v2{33,44};
S s;
s.v = v2;
n_s[8] = s;
}
m_n_s[9] = n_s;
}
S & s = m_n_s.at(9).at(8);
std::cout << s.v.size() << '\n'; // size: 2
std::cout << s.v.at(0) << '\n'; // 33
std::cout << std::get<0>(s.tup).size(); // size: 2(gcc,clang) 0(MSVC)
}