11

At the moment, boost::optional<> supports references but the std::experimental::optional<> on my system from libstdc++ does not. Is this reflective of what might make it into the standard?

I know that the optional proposal author spun off optional references as a separate proposal so that the main optional proposal would have a better chance of being accepted. Was the proposal for optional references rejected or did work on it stop?

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • 3
    One can always use `std::reference_wrapper`. Supporting "raw" references would open a whole dimention of possibilities for shooting oneself into the foot. – bobah Jun 19 '16 at 08:46
  • 1
    @bobah I'd mention your comment in my answer. Is it fine for you? – skypjack Jun 19 '16 at 08:48
  • 2
    @skypjack - sure :-) – bobah Jun 19 '16 at 08:49
  • 1
    @bobah Good suggestion. Is there a reason optional references are more likely to end up dangling than regular references? I'm not seeing it. – Praxeolitic Jun 19 '16 at 09:03
  • 1
    @Praceolitic - it is not as easy to unintentionally stick a reference wrapper to a container as plain reference and it's unambiguous what to do when the container is assigned a value of type `T` or a value of type `reference_wrapper()` – bobah Jun 19 '16 at 09:12

1 Answers1

8

Is this reflective of what might make it into the standard?

From the working draft ([20.5.2/1]):

A program that necessitates the instantiation of template optional for a reference type [...] is ill-formed.

I guess this replies to your question.

Note that you can still work around it by using std::reference_wrapper, as mentioned by @bobah in the comments.

skypjack
  • 49,335
  • 19
  • 95
  • 187