I came across the following syntax for the push_back
function. Vertex
is just a structure that holds the three floats x, y and z. The second line looks just like would write it. But the first line looks weird to me. In the video where this was explained it was said that this is done with the member initializer list but it looks more like implicit conversion. I am just confused by the curly brackets there. Can anyone explain why this syntax works?
std::vector<Vertex> vertices;
vertices.push_back({ 1, 2, 3 });
vertices.push_back(Vertex(1, 2, 3));
This is not member initializer list but copy list initialization (since C++11).
7) in a function call expression, with braced-init-list used as an argument and list-initialization initializes the function parameter
vertices.push_back()
expects an Vertex
as the argument, the braced-init-list { 1, 2, 3 }
is used to construct a temporary Vertex
which is passed to push_back
later. As you said, you can think it as implicit conversion too, i.e. the conversion from the braced-init-list to Vertex
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With