I have std::variant
where all classes are derived from the same base. I want to cast variant to base.
return std::visit( []( const Base& b ) { return b; }, v );
This compiles but gives warning C4172: returning address of local variable or temporary
Is there a way to visit std::variant
in place, without making local or temporary copies?
Or if it’s impossible, how can I cast the value to void*
so I can use static_cast
?
Update: I thought the example should be obvious, but it's not, here's the complete repro:
#include <variant>
struct Base {};
struct A : Base {};
struct B : Base {};
const Base& cast( const std::variant<A, B>& v )
{
return std::visit( []( Base const& b ) { return b; }, v );
}
int main()
{
std::variant<A, B> v{ A{} };
const auto& b = cast( v );
}