#include <iostream>
#include <variant>
using namespace std;
template<int nd>
class base {};
template<int nd>
class base_holder : public base<nd>{
public:
base_holder () {
cout<<"new base_holder<"<<nd<<"> = "<<this<<endl;
}
~base_holder () {
cout<<"del base_holder<"<<nd<<"> = "<<this<<endl;
}
};
std::variant<base_holder<1>, base_holder<2>, base_holder<3>>
new_base_holder(int nd) {
switch (nd) {
case 1: return base_holder<1>();
case 2: return base_holder<2>();
case 3: return base_holder<3>();
default: throw "";
}
}
int main(int argc, char **argv){
auto cc = new_base_holder(2);
cout<<"tt"<<endl;
return 0;
}
А на выходе:
new base_holder<2> = 0x7ffe8b143a77
del base_holder<2> = 0x7ffe8b143a77
tt
del base_holder<2> = 0x7ffe8b143aa6