I have the following code:
typedef boost::variant<LandSearchParameter, WaterSearchParameter> SearchParameter;
enum Visibility{
CLEAR,
CLOUDY,
FOG,
SMOKE
};
class DetectionGenerator : public boost::static_visitor<double>{
public:
DetectionGenerator(const EnvironmentalFactors& factors);
double operator()(const LandSearchParameter& land, Visibility vis) const;
double operator()(const WaterSearchParameter& water, Visibility vis) const;
private:
const EnvironmentalFactors mFactors;
};
but if I try to use it with boost::apply_visitor in the following manner:
SearchParameter param = globeCover.generateSearch(lat, lon, altitude);
Visibility vis = weather.generateVisibility(lat, lon, altitude, bearing);
DetectionGenerator detectGen(envFactors);
double prob = boost::apply_visitor(detectGen, param, vis);
and get the following from gcc:
error: no matching function for call to
‘apply_visitor(const SearchRescue::DetectionGenerator&, const boost::variant<boost::tuples::tuple<double, double, double, double, double, bool, bool, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::tuples::tuple<std::size_t, std::size_t, double, double, double, bool, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>&, SearchRescue::Visibility)
If I attempt to wrap the Visibility enum within a boost::variant I get the same error only instead of Visibility it reads all that junk above and whatever name I chose for the variant. I’ve read over the docs on boost for binary visitation but I’m at a loss. Due note, all these things are within the same namespace.
Update:
It was my attempt that was the problem. Not shown above was that I had the visitor as a const variable. Once I took the const out of the picture, it compiled. Thank you all for trying to help me out. Wish I could give more upvotes.
@Boaz Yaniv’s answer is 100% correct. The
boost::apply_visitor<>docs state directly:Yaniv’s suggested approach for remedying that – taking a
Visibilityobject in the visitor’s constructor – is also the proper fix. You indicated that such an approach did not work for you; I’ll warrant that the problem was in your attempt and not in the approach. ;-] Here’s code that compiles:If this approach continues to fail to work for you then you’ll need to edit your question and update it with your actual, current code.
P.S. Your approach of making
Visibilitya single-typeboost::variant<>should work also, though it seems silly to me. For reference, this compiles: