0

std::optional provides constructors that forwards arguments to the constructor of the owned object:

 template<class...Args>
 optional(in_place_t,Args&&...args)

But it also provides this overload:

 template<class U,class...Args>
 optional(in_place_t,initializer_list<U> l,Args&&...args)

What are the benefits of this last overload?

T.C.
  • 133,968
  • 17
  • 288
  • 421
Oliv
  • 17,610
  • 1
  • 29
  • 72

1 Answers1

4

It's to allow you to do this:

 optional<vector<int>> o(in_place_t, {1, 2, 3, 4, 5});

That's much shorter than:

 optional<vector<int>> o(in_place_t, std::initializer_list<int>{1, 2, 3, 4, 5});
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982