The following code is pretty self explanatory, I'm getting some compiler errors when trying to compile it. Do you know what changes I need to make to get this to compile? I've simplified the example, so you don't have to do too much reading.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <memory>
#include <variant>
usingnamespace std;
int main( int argc, char *argv[] ){
using IntOrChar = variant< int, char >;
shared_ptr< IntOrChar > value( new IntOrChar( (int)1 ) );
if( auto i = get_if< int >( *value ) )
cout << i << endl;
}
The previous code doesn't compile for some reason, and yet the following does:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <memory>
#include <variant>
usingnamespace std;
int main( int argc, char *argv[] ){
using IntOrChar = variant< int, char >;
shared_ptr< IntOrChar > value( new IntOrChar( (int)1 ) );
if( holds_alternative< int >( *value ) )
cout << get< int >( *value ) << endl;
}
I'd rather use std::get_if, if I can, because it's less lines of code for what I'm programming, and holds_alternative is a long function name.