Why is a templated variant return type cause bad_variant_access exception?
Unhandled exception at 0x779508B2 in Project2.exe: Microsoft C++ exception: std::bad_variant_access at memory location 0x00B3FB00.
When in another program, which is bigger, the statement in main got executed without error, but prints out some strange value other than 142. which seemingly is an Undefined behavior.
/////////////////////minified_main.cpp/////////////////
#include <iostream>
#include <variant>
template <typename T>
class THolder {
public:
THolder() = default;
THolder(T p) : t_(p) {}
THolder(const THolder<T>& other) = default;
public:
T t_;
};
class THolderProducer {
public:
THolderProducer() = default;
std::variant<THolder<int>, char*>&& operator()() { return std::move(std::variant<THolder<int>, char*>(THolder<int>{142})); }
};
int main(int argc, char* argv[]) {
std::cout <<'[' << std::get<0>(THolderProducer()()).t_ << std::endl;
return 0;
}