As a C++ programmer, I have been taught the simple rule of passing parameters:
Passing parameter
T
by value only whensizeof(T) <= sizeof(void*)
, except for move_in or output.
However, the C++ standard library seems not comply with the rule. For example, sizeof(std::initializer_list<T>)
is greater than sizeof(void*)
, but std::vector
has a constructor:
vector(std::initializer_list<T>, const Allocator&);
Why does the C++ standard library always pass std::initializer_list<T>
by value rather than by reference?
Aucun commentaire:
Enregistrer un commentaire