CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
#include <variant>
class test
{
int a;
public:
explicit test(int i) : a(i) {}
};
int main()
{
variant<int,test> v1;//compiles as int is default constructible
variant<test,int> v2(test(3));//compiles as object is passed to the v2 variant
variant<test,int> v3(3);//compiles as test object is created by implicit conversion. had we marked test constructor explicit , it will not compile
//variant<test,int> v4;//default constructor is not available and hence not compile
variant<monostate,test,int> v5;
cout << "monostate allow to avoid first type to be constructed unless specified";
return 0;
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run