Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::initializer_list and reference types

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.

like image 800
user1095108 Avatar asked Jun 08 '14 18:06

user1095108


2 Answers

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 type const 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.

like image 122
David G Avatar answered Oct 01 '22 16:10

David G


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

like image 29
Marco A. Avatar answered Oct 01 '22 16:10

Marco A.