I have declared a boost::variant
which accepts three types: string
, bool
and int
. The following code is showing that my variant accepts const char*
and converts it to bool
. Is it a normal behavior for boost::variant
to accept and convert types not on its list?
#include <iostream>
#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"
using namespace std;
using namespace boost;
typedef variant<string, bool, int> MyVariant;
class TestVariant
: public boost::static_visitor<>
{
public:
void operator()(string &v) const
{
cout << "type: string -> " << v << endl;
}
template<typename U>
void operator()(U &v)const
{
cout << "type: other -> " << v << endl;
}
};
int main(int argc, char **argv)
{
MyVariant s1 = "some string";
apply_visitor(TestVariant(), s1);
MyVariant s2 = string("some string");
apply_visitor(TestVariant(), s2);
return 0;
}
output:
type: other -> 1
type: string -> some string
If I remove the bool type from MyVariant and change it to this:
typedef variant<string, int> MyVariant;
const char*
is no more converted to bool
. This time it's converted to string
and this is the new output:
type: string -> some string
type: string -> some string
This indicates that variant
tries to convert other types first to bool
and then to string
. If the type conversion is something inevitable and should always happen, is there any way to give conversion to string
a higher priority?