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.)
2 Answers
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.
-
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 forboost::optional
?– VahagnCommented 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? ։)– VahagnCommented 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 anoptional<optional<T&>>
? Would doingot = nullopt
disengage the outer wrapper or the inner wrapper? Commented Nov 2, 2016 at 16:55 -
1In my opinion.
optional<T&>
is NOT equivalent to aT*
. In modern C++ thoughts, if you return aT*
, 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 aoptional<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 -
2Why 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.– NuclearCommented Apr 29, 2021 at 7:51
In [optional]:
A program that necessitates the instantiation of template optional for a reference type, or for possibly cv-qualified types
in_place_t
ornullopt_t
is ill-formed.
There is no std::optional<T&>
. For now, you'll have to use std::optional<std::reference_wrapper<T>>
.
-
will this support
->
? Thestd::reference_wrapper
doesn't seem to implementoperator->
. Soo->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 thereference_wrapper
. I do hope that eventually we getoptional<T&>
that rebinds on assignment, I've become convinced by many people that that's the right behavior.– BarryCommented 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&>
intostd::optional<std::reference_wrapper<T>>
, but it automatically unwraps the reference onoperator->
andoperator*
.operator=
will rebind the reference. Commented Aug 23, 2019 at 2:35
optional
works. Otherwise, you couldn't haveoptional<non_default_constructible>
either. You could write an optional type that supports reference types. Boost's does. The standard just chooses not to (due tooperator=
).std::optional< std::reference_wrapper<int> >
:-Dstd::optoinal<std::variant<T&>>
։)