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.
  • 129,563
  • 16
  • 274
  • 404
Oliv
  • 17,016
  • 1
  • 26
  • 65

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
  • 413,367
  • 61
  • 711
  • 904