How to get a variable from variant type, if you don't know a type of variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get a variable from variant type, if you don't know a type of variable?

Say, for example, I have a this: std::variant<int, std::string> v; I am assigning 5 to this variable: v = 5; or "Hello World": v = "Hello World"; but, what if this depended on the user input and I need to cout it? I dont know the type... the question is, how can I know a type that I want to get from variant type? Is there even a way?

5th Mar 2021, 4:07 PM
Arslan Annayew
Arslan Annayew - avatar
2 Answers
+ 2
I think user will not input other than string or int variable .with typeof method you can check what type of v variable cout<<typeof(v); Compiler will automatically generat data type for the variant variable based in user input.
5th Mar 2021, 4:16 PM
HBhZ_C
HBhZ_C - avatar