Reputation: 976
When trying to compare two strings, one wrapped in a boost::variant type, it works only half of the time. If my boost::variant contains string, int, double, and bool, it doesn't work (it gives me this error: libc++abi.dylib: terminating with uncaught exception of type boost::wrapexcept<boost::bad_get>: boost::bad_get: failed value get using boost::get Abort trap: 6
). But if my boost::variant contains just a string and an int, it works just fine! This makes no sense to me. If you have any idea why this is happening, and how to fix it, please let me know.
// variant_equality.cpp
#include <boost/variant.hpp>
#include <string>
#include <iostream>
// doesn't work: #define varied_type boost::variant<std::string, int, double, bool>
// does work: #define varied_type boost::variant<std::string, int>
bool variant_equal(varied_type v, std::string normal) {
try {
return boost::get<std::string>(v) == normal;
}
catch (boost::bad_get) {
return false;
}
}
int main() {
varied_type bar = "foo";
std::cout << variant_equal(bar, "foo") << std::endl;
return 0;
}
Upvotes: 1
Views: 27