I am implementing a visitor in order to use it with the boost variant library.
I want to know if is correct to specialize boost::static_visitor<> with a const reference type.
Note my question here is the following:
There are any problem specializing boost::static_visitor<> to boost::static_visitor<const T&> ?
template<typename T>
struct my_visitor : public boost::static_visitor<const T&> {
template<typename U> const T& operator()(U& u) const {
// some code here .....
return X<U>::get_some_t(); // finally return some T.
}
};
There is no problem as long as you don't return a reference to a local/temporary.
Also, be sure to check the validity of the reference over time (it ends when the variant object is destructed, which is when the variant itself is destructed, or (!) when it is reinitialized).
Background and explanation
A variant contains an object of the "current" element type, and you can reference into that object perfectly fine. As long as the variant is not reinitialized to another element type (in which case the reference is "just" dangling, exactly like it would if the lifetime of the referred-to object had ended).
So if
get_somet_t()returns aT&orT const&(or something with a suitable implicit conversion) there is no problem.In a simpler setting let me demonstrate valid options:
Likewise, you can even make variants of /just references/:
See it all Live On Coliru
Demonstration
Put yet another way, the following is fine:
See it Live On Coliru as well.