I am trying to loop over the entries of a map, and I get unexpected copies. Here is the program:
#include <iostream>
#include <map>
#include <string>
struct X
{
X()
{
std::cout << "default constructor
";
}
X(const X&)
{
std::cout << "copy constructor
";
}
};
int main()
{
std::map<int, X> numbers = {{1, X()}, {2, X()}, {3, X()}};
std::cout << "STARTING LOOP
";
for (const std::pair<int, X>& p : numbers)
{
}
std::cout << "ENDING LOOP
";
}
And here is the output:
default constructor
copy constructor
default constructor
copy constructor
default constructor
copy constructor
copy constructor
copy constructor
copy constructor
STARTING LOOP
copy constructor
copy constructor
copy constructor
ENDING LOOP
Why do I get three copies inside the loop? The copies disappear if I use type inference:
for (auto&& p : numbers)
{
}
What's going on here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…