gpt4 book ai didi

c++ - boost 二进制 static_visitor 和 apply_visitor

转载 作者:可可西里 更新时间:2023-11-01 18:16:32 25 4
gpt4 key购买 nike

我有以下代码:

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;
};

但如果我尝试将它与 boost::apply_visitor 一起使用通过以下方式:

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);

并从 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)

如果我尝试包装 Visibility boost::variant 中的枚举我只得到同样的错误而不是 Visibility它读取上面的所有垃圾以及我为变体选择的任何名称。我已经阅读了有关 binary visitation 的 boost 文档但我不知所措。请注意,所有这些东西都在同一个命名空间内。

更新:

问题是我的尝试。上面没有显示的是我有访客作为 const多变的。一旦我拿了 const出图了,就编译了。谢谢大家试图帮助我。希望我能给予更多赞成票。

最佳答案

@Boaz Yaniv 的回答 100% 正确。 boost::apply_visitor<> docs直接声明:

Overloads accepting two operands invoke the binary function call operator of the given visitor on the content of the given variant operands.

Yaniv 建议的补救方法 – 采用 Visibility访问者构造函数中的对象——也是正确的解决方法。您表示这种方法对您不起作用;我保证问题出在您的尝试中,而不是方法中。 ;-] 下面是编译代码:

#include <boost/variant.hpp>

struct LandSearchParameter { };
struct WaterSearchParameter { };
struct EnvironmentalFactors { };

typedef boost::variant<
LandSearchParameter,
WaterSearchParameter
> SearchParameter;

enum Visibility
{
CLEAR,
CLOUDY,
FOG,
SMOKE
};

struct DetectionGenerator : boost::static_visitor<double>
{
DetectionGenerator(EnvironmentalFactors const& factors, Visibility vis)
: mFactors(factors),
mVis(vis)
{ }

double operator ()(LandSearchParameter const&) const { return 0.; }
double operator ()(WaterSearchParameter const&) const { return 0.; }

private:
EnvironmentalFactors mFactors;
Visibility mVis;
};

int main()
{
SearchParameter param = LandSearchParameter();
EnvironmentalFactors const envFactors;
DetectionGenerator const detectGen(envFactors, CLOUDY);
double const prob = boost::apply_visitor(detectGen, param);
}

如果这种方法仍然对您不起作用,那么您将需要编辑您的问题并使用您当前的实际代码对其进行更新。

附言你的制作方法Visibility单型boost::variant<>也应该工作,虽然这对我来说似乎很愚蠢。作为引用,这个编译:

#include <boost/variant.hpp>

struct LandSearchParameter { };
struct WaterSearchParameter { };
struct EnvironmentalFactors { };

typedef boost::variant<
LandSearchParameter,
WaterSearchParameter
> SearchParameter;

enum VisibilityT
{
CLEAR,
CLOUDY,
FOG,
SMOKE
};

typedef boost::variant<VisibilityT> Visibility;

struct DetectionGenerator : boost::static_visitor<double>
{
explicit DetectionGenerator(EnvironmentalFactors const& factors)
: mFactors(factors)
{ }

double operator ()(LandSearchParameter const&, VisibilityT const) const
{ return 0.; }

double operator ()(WaterSearchParameter const&, VisibilityT const) const
{ return 0.; }

private:
EnvironmentalFactors mFactors;
};

int main()
{
SearchParameter param = LandSearchParameter();
EnvironmentalFactors const envFactors;
DetectionGenerator const detectGen(envFactors);
Visibility vis = CLOUDY;
double const prob = boost::apply_visitor(detectGen, param, vis);
}

关于c++ - boost 二进制 static_visitor 和 apply_visitor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com