If we create a simple dummy-class as follows:
struct example {
example() { std::cout << "Create" << std::endl; }
example(const exam&) { std::cout << "Copy" << std::endl; }
example(example &&) noexcept { std::cout << "Move" << std::endl; }
}
And pass it in an initialization list (std::initializer_list<example>
):
some_function({example()});
The output (from GCC/C++11) is:
Create
Copy
Copy
This doesn't make sense to me as you create the example object itself then pass it through an initializer_list
. This means that you have the original object, your list has a copy of the object and your function has a copy of the list hinting that it should be a single copy of your object then a single copy of the initializer list.
Expected output (without the extra copy):
Create (from example())
Copy (into initializer_list)
TL;DR: Why does passing my object through an initializer list instantiate two copies of the object instead of one if the object getting passed in the initializer list itself?
Aucun commentaire:
Enregistrer un commentaire