I was learning boost's variant and found that it makes implicit conversion from char* to std::string in this snippet:
boost::variant<int, char*, std::string> v; // implicitly converted from char* to std::string, so we can't store char* and std::string both? v = "123"; std::cout << "boost::variant value: " << boost::get<std::string>(v) << std::endl; v = 1; std::cout << "boost::variant value: " << boost::get<int>(v) << std::endl; v = "123"; std::cout << "boost::variant value: " << boost::get<char*>(v) << std::endl; So the exception will be raised by the boost::get<char*>(v) call. I wondered a bit and tried to make it more fair: change char* to const char* in the typename list:
boost::variant<int, const char*, std::string> v; v = "123"; std::cout << "boost::variant value: " << boost::get<std::string>(v) << std::endl; Now exception is correct. But my question is: why compiler/library chooses std::string instead of char* as I specified? I understand, that v = "123" is assignment of const char* but why it converts to std::string and not char*?
char*andconst char*, is not it?const char *tochar *(it doesn't, luckily), that would have no impact on C++. If C makes"123"achar[4]rather thanconst char[4](it does, unfortunately), that too has no impact on C++ and is not relevant to your question.