Can a std::initializer_list
contain reference types (both rvalue and lvalue)? Or does one have to use pointers or a reference wrapper (such as std::ref
)?
EDIT:
Perhaps more clarification is due:
I have a member variable, ::std::vector<std::function<void()> >
, into which I would like to forward a lambda object. This would usually be accomplished with emplace_back
, but I wanted to do it in the constructor's initialization list. Alas, as I read, this would make forwarding impossible.
Can a
std::initializer_list
contain reference types (both rvalue and lvalue)?
std::initializer_list<T>
doesn't hold references to its elements. It uses copy-semantics by holding its values as const
objects:
18.9
Initializer List[support.initlist]
An object of type
initializer_list<E>
provides access to an array of objects of typeconst E
.
An initializer_list
of references will cause a compilation error because iternally pointers are used for iterators:
#include <initializer_list>
int main()
{
int x;
std::initializer_list<int&> l = {x};
// In instantiation of 'class std::initializer_list<int&>':
// error: forming pointer to reference type 'int&'
// typedef const _E* iterator;
}
An initializer_list
also doesn't support move-semantics as const
objects cannot be moved from. Holding your objects in a std::reference_wrapper<T>
is the most viable solution if you wish to maintain reference-semantics.
From http://www.cplusplus.com/reference/initializer_list/initializer_list/
initializer_list objects are automatically constructed as if an array of elements of type T was allocated
thus they can't be used with something like std::initializer_list<int&>
. The reason is the same for which the following gives a compiler error
int& arr[20];
error: declaration of ‘arr’ as array of references
and that is dictated by the C++ standard: https://stackoverflow.com/a/1164306/1938163
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