I am having issue when trying to pass a reference object inside std::optional as a function argument.Does it not support storing reference objects ? Example -
void fun(std::optional<ClassA& obj>)
What exatly does this error mean -
static_assert failed due to requirement '!is_reference
static_assert(!is_reference_v<_Tp>)
The cpp reference states quite clearly:
There are no optional references; a program is ill-formed if it instantiates an optional with a reference type. Alternatively, an optional of a std::reference_wrapper of type T may be used to hold a reference.
So use std:reference_wrapper
as suggested.
Footnote: there are movement in the cpp commitee to enable references in std::optional
. We might get them in future.