只需包括缺少的重载:
住在科利鲁
#include <boost/variant.hpp>
#include <iostream>
using TypeVariant = boost::variant<int, float, bool>;
struct ConvertToBool {
using result_type = TypeVariant;
TypeVariant operator()(TypeVariant const& v) const {
return boost::apply_visitor(*this, v);
}
TypeVariant operator()(int a) const { return a != 0; }
TypeVariant operator()(float a) const { return a != 0; }
TypeVariant operator()(bool a) const { return a; }
} static constexpr to_bool{};
int main() {
using V = TypeVariant;
for (V v : {V{}, {42}, {3.14f}, {true}}) {
std::cout << v << " -> " << std::boolalpha << to_bool(v) << "\n";
}
}
概括
在更一般的情况下,您可以提供包罗万象的模板重载:
template <typename T> TypeVariant operator()(T const& a) const {
return static_cast<bool>(a);
}
事实上,在你的微不足道的情况下,这就是你所需要的:
住在科利鲁
#include <boost/variant.hpp>
#include <iostream>
using TypeVariant = boost::variant<int, float, bool>;
struct ConvertToBool {
using result_type = TypeVariant;
TypeVariant operator()(TypeVariant const& v) const {
return boost::apply_visitor(*this, v);
}
template <typename T> TypeVariant operator()(T const& a) const {
return static_cast<bool>(a);
}
} static constexpr to_bool{};
int main() {
using V = TypeVariant;
for (V v : { V{}, { 42 }, { 3.14f }, { true } }) {
std::cout << v << " -> " << std::boolalpha << to_bool(v) << "\n";
}
}
仍然打印
0 -> false
42 -> true
3.14 -> true
true -> true