私はテストしている次のコードを持っています:
typedef boost::variant<int, std::string> Type;
typedef boost::variant<std::vector<int>, std::vector<std::string> > Container;
class Setter: public boost::static_visitor<>
{
public:
Setter(Container& container): _container(container)
{
}
template <class T>
void operator()(const T& valueToSet)
{
std::vector<T> *container = boost::get< std::vector<T> >(&_container);
if(container->size()==0)
{
container->resize(1);
}
(*container)[0] = valueToSet;
}
private:
Container& _container;
};
次の単体テストを使用します。
TEST_F (TestSet, addIncorrectTypeToContainer)
{
Container container((std::vector<std::string>()));
Setter setter = Setter(container);
ASSERT_THROW(boost::apply_visitor(setter, Type(int(1))), boost::bad_get);
}
boost::bad_get 例外が発生しません。代わりに NULL を返します。
私は何を間違っていますか?