I am trying to work with boost::variant and am running into a bit of a problem with the way I am using it. I was hoping someone could shed some light on the situation.
I have created a boost::variant and templatized it with both an int
and an unsigned int
. When I assign a numeric value, (ie. 4) to the variant, I would expect the compiler to complain since plain old 4 cannot have it's type inferred unambiguously. How does this even compile?! How does the compiler choose the type?
Is there a way to make the compiler complain about these sorts of things?
#include <stdint.h>
#include <boost/variant.hpp>
#include <boost/scoped_ptr.hpp>
#include <cxxabi.h>
struct MyComplexType
{
int myInt;
uint32_t myUint;
};
int main()
{
boost::variant< MyComplexType, int, uint32_t, float, std::string> myAmbiguousVar;
myAmbiguousVar = 4; // <- ?? My compiler chooses this to be an int
int status;
boost::scoped_ptr<char> pDemangled(__cxxabiv1::__cxa_demangle(myAmbiguousVar.type().name(), 0, 0, &status));
std::cout << std::string(pDemangled.get()) << std::endl;
}