13

std::optional<int&> xx; just doesn't compile for the latest gcc-7.0.0 snapshot. Does the C++17 standard include std::optional for references? And why if it doesn't? (The implementation with pointers in a dedicated specialization whould cause no problems i guess.)

5
  • 1
    @Someprogrammerdude That's not how optional works. Otherwise, you couldn't have optional<non_default_constructible> either. You could write an optional type that supports reference types. Boost's does. The standard just chooses not to (due to operator=).
    – Barry
    Commented Nov 2, 2016 at 15:05
  • 3
    Pretty much the entire point of references is, they're not optional.
    – jthill
    Commented Nov 2, 2016 at 21:47
  • 2
    How about std::optional< std::reference_wrapper<int> > :-D
    – M.M
    Commented Nov 2, 2016 at 21:51
  • @M.M Actually in my code it turned out more convenient to do std::optoinal<std::variant<T&>> ։)
    – Vahagn
    Commented Nov 8, 2016 at 14:50
  • 1
    Possible duplicate of std::optional specialization for reference types
    – jww
    Commented Sep 21, 2018 at 0:24

2 Answers 2

19

Because optional, as standardized in C++17, does not permit reference types. This was excluded by design.

There are two reasons for this. The first is that, structurally speaking, an optional<T&> is equivalent to a T*. They may have different interfaces, but they do the same thing.

The second thing is that there was effectively no consensus by the standards committee on questions of exactly how optional<T&> should behave.

Consider the following:

optional<T&> ot = ...;
T t = ...;
ot = t;

What should that last line do? Is it taking the object being referenced by ot and copy-assign to it, such that *ot == t? Or should it rebind the stored reference itself, such that ot.get() == &t? Worse, will it do different things based on whether ot was engaged or not before the assignment?

Some people will expect it to do one thing, and some people will expect it to do the other. So no matter which side you pick, somebody is going to be confused.

If you had used a T* instead, it would be quite clear which happens:

T* pt = ...;
T t = ...;
pt = t;   //Compile error. Be more specific.
*pt = t;  //Assign to pointed-to object.
pt = &t;  //Change pointer.
10
  • Well, I wouldn't accept the first reason, as i need a uniform interface in a generic code. As for the second reason, for me it should definitely behave like *ot = t. By the way, is it the case for boost::optional?
    – Vahagn
    Commented Nov 2, 2016 at 16:14
  • 2
    "So no matter which side you pick, somebody is going to be confused." -- So the solution is to make the both sides confused right? ։)
    – Vahagn
    Commented Nov 2, 2016 at 16:15
  • 1
    @Vahagn: "for me it should definitely behave like *ot = t." If you do that, how would you rebind a reference? Set it to NULL and then set it to the new reference? And how would the latter work? What if you have an optional<optional<T&>>? Would doing ot = nullopt disengage the outer wrapper or the inner wrapper? Commented Nov 2, 2016 at 16:55
  • 1
    In my opinion. optional<T&> is NOT equivalent to a T*. In modern C++ thoughts, if you return a T*, the caller may think whether I should free the pointer after I use it. The raw pointer may confuse by who is the ownership of the object. But if you return a optional<T&>, the caller will not deal with the optional, because it's may either none or a reference. Commented Jun 2, 2020 at 6:49
  • 2
    Why not just disable the assignment opertor for std::optional? std::optional should be designed to be constructed and passed to function or returned from a function. Not for reassigning it's value, which leads to such problems.
    – Nuclear
    Commented Apr 29, 2021 at 7:51
5

In [optional]:

A program that necessitates the instantiation of template optional for a reference type, or for possibly cv-qualified types in_place_t or nullopt_t is ill-formed.

There is no std::optional<T&>. For now, you'll have to use std::optional<std::reference_wrapper<T>>.

3
  • will this support ->? The std::reference_wrapper doesn't seem to implement operator->. So o->foo will not work. Commented Jul 18, 2017 at 15:17
  • 1
    @JohannesSchaub-litb You'd need two indirections - like o->get().foo, cause -> just gives you the reference_wrapper. I do hope that eventually we get optional<T&> that rebinds on assignment, I've become convinced by many people that that's the right behavior.
    – Barry
    Commented Jul 18, 2017 at 15:23
  • @JohannesSchaub-litb I came to the same conclusion, so I wrote a wrapper class, and ended up here to see if anyone had any similar ideas. This translates std::optional<T&> into std::optional<std::reference_wrapper<T>>, but it automatically unwraps the reference on operator-> and operator*. operator= will rebind the reference.
    – monkey0506
    Commented Aug 23, 2019 at 2:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.