【问题标题】:C++ boost::get and visitorC++ boost::get 和访问者
【发布时间】:2018-08-23 11:12:35
【问题描述】:

我制作了一个带有多种类型的boost::variant 的向量,如下所示:

std::vector<boost::variant<type1,type2,type3,...and so on>>

然后,我创建了一个 typedef 将整个类型重命名为 vecTypesVar 和名为 typesVar 的变体

typedef boost::variant<type1,type2,type3,...and so on> typesVar
typedef std::vector<typesVar> vecTypesVar

现在我已经在该向量上创建了一个 for 循环,并使用选项 .which 我正在尝试为每个类型进行这样的实现。

vecTypesVar type = getTypesData();
for (auto i = 0; i < type.size(); ++i)
{
    switch (type[i].which())
    {
    case 0:
        break;

    case 1:
        break;

    case 2:
        break;
    default: ;
    }
}

所以,问题就从这里开始了,在 switch 的每个 case 上,我都试图使变量的类型与出现的变量相同。 p>

例如:

case 0:
    auto data = boost::get<type1>(type[i]);

但我收到一条错误消息:

错误 C2248: 'type1::type1' : 无法访问 在“type1”类中声明的私有成员

我也尝试过像这样使用访问者: 模板

class port_visitator : public boost::static_visitor<T>
{
    T operator()(typesVar type) const
    {
        return boost::get<T>(type);
    }
};

并在我的开关中使用它:

case 0:
   auto data = apply_visitor(port_visitator<type1>(), type[i]);

但我收到同样的错误:

错误 C2248: 'type1::type1' : 无法访问私有 在“type1”类中声明的成员

编辑: 这是 type1 的构造函数:

template<typename T, dataType ST>
class type1: public sometype<T, ST>, public othertype<T, ST>, public anothertype
{
public:
    template <typename T1>
    type1(
        CSimSwcBase*           _plgInstance,
        const char*            _name,
        const T1&              _initialValue,
        const long             _version,
        const SimFunc_t        _eventFunc           = NULL,
        void*                  _eventFuncInstance   = NULL,
        const long             _size                = GetSize<T>::SIZE,
        const unsigned long    _portMode            = 0,
        const bool             _definedDefaultValue = false,
        void*                  _pDefaultValue       = NULL,
        const unsigned long    _defaultValueSize    = 0,
        const bool             _bBuffered           = false)
        : sometype(_plgInstance, _name, _initialValue, _size, _portMode, _eventFunc, _eventFuncInstance, _definedDefaultValue, _pDefaultValue, _defaultValueSize, _bBuffered)
        , othertype(dynamic_cast<sometype*>(this))
        , m_bUseSyncRef (false)
        , m_bGetDefaultValues(false)
        , m_bReportError(true)
        , m_errorCount(0)
        , m_maxError(100)
        , m_bDoOnlyStartupTest(false)
        , m_bStartupTestPassed(false)
        , m_bNotConnectedReported(false)
    {
        setupVersion(_version);
    }
}

有没有人知道我该如何解决这个问题?

提前致谢:D

【问题讨论】:

    标签: c++ visual-studio c++11 boost


    【解决方案1】:

    在没有足够清晰的情况下需要在此处实际构建和复制:

    auto data = boost::get<type1>(type[i]);
    

    你可以简单地使用这样的参考:

    auto& data = boost::get<type1>(type[i]);
    

    从而完全消除了构建的需要。

    【讨论】:

    • 不可能,我只是检查一下它的构造函数,它是这样公开的。 class type1: public sometype&lt;T, ST&gt;, public othertype&lt;T, ST&gt;, public otherothertypepublic: template &lt;typename T1&gt; type1(...){....}
    • @Vali 您评论中的这段代码不是构造函数——它是继承的声明。
    • @Vali OK 检查它
    • @Vali 好的,您的复制构造函数或 operator= 是否为 private=deleteed?
    • @Vali 有趣——这可以编译吗? auto&amp; data = boost::get&lt;type1&gt;(type[i]);?
    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多