最佳答案
In almost every post I see on SO, involving a std::initializer_list
, people tend to pass a std::initializer_list
by value. According to this article:
https://web.archive.org/web/20120707045924/cpp-next.com/archive/2009/08/want-speed-pass-by-value/
one should pass by value, if one wants to make a copy of the passed object. But copying a std::initializer_list
is not a good idea, as
Copying a
std::initializer_list
does not copy the underlying objects. The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended.
So why is an instance of it often passed by value and not by, say const&
, which guaranteed does not make a needless copy?