Boost :: variant的多态设置器

好奇尼克

我正在尝试使用boost :: variant与模板类型。例如,我有一个模板类型Tag<T>和升压::变体AnyTag包括类型,如Tag<double>Tag<int>Tag<std::string>每个Tag<T>变量具有类型T的成员。现在,我想将这些变量放入容器中,并在运行时简单地分配值,例如,

for(AnyTag & tag: AllTags) {
    setValue(tag, getValueFromXml()); 
}

该函数setValue(AnyTag &tag, T &val)必须使用AnyTag标记的运行时类型,以便为标记正确分配正确的值。我尝试解决该问题的方法可以在下面找到,它利用了另一个变体,该变体仅包含可以在AnyTag(TagValueType)中使用的可能的T类型。

template<typename T, typename = void>
class Tag {};

template <typename T>
class Tag<T, EnableIf<std::is_arithmetic<T>>> {
public:
    T value = 0;
    std::string address = "";
    T maxValue = std::numeric_limits<T>::max();
    typedef T value_type;
};

template <typename T>
class Tag<T, DisableIf<std::is_arithmetic<T>>> {
public:
    T value;
    std::string address = "";
    typedef T value_type;
};

typedef boost::variant<Tag<std::string>,
                       Tag<double>,
                       Tag<int>,
                      > AnyTag;

typedef boost::variant<std::string, double, int> TagValueType;

class tag_set_value_visitor: public boost::static_visitor<void>
{
    const TagValueType & value;
public:
    tag_set_value_visitor(const TagValueType & val): value(val){}
    template <typename T>
    void operator()(T & tag) const
    {
        tag.value = boost::get<typename T::value_type>(value);
    }
};

inline void setValue(AnyTag & tag, const TagValueType & val) {
    assert(tag.which() == val.which());
    boost::apply_visitor( tag_set_value_visitor(val), tag );
}

不幸的是,这种方法不是我想要的,因为例如在编译期间,如果我执行以下操作就没有问题:

AnyTag a = Tag<int>();
setValue(a, double(1.3));

但是在运行时,boost库会检测到类型不匹配并使程序崩溃。

因此,我的解决方案是一种将问题推迟的类型擦除。

我想拥有一个setValue(AnyTag&tag,T&val),其中T是AnyTag的运行时类型。

我知道那是变体的访问者试图做的事情,但是在这种情况下有一个问题,因为在构造访问者时,我们必须知道我们将要使用的类型。

关于这个问题有什么想法或想法吗?

PS:很长的帖子,很抱歉,但是我找不到用更少的单词来解释我的思维过程的方法。

看到

使用¹二进制访问者。

实现,operator()除了对应的类型外什么也不做。

处理口味不匹配(我返回一个表示成功的布尔值):

Live On Coliru

#include <boost/any.hpp>
#include <boost/variant.hpp>
#include <boost/mpl/vector.hpp>
#include <string>

using namespace boost;

template <typename T>
struct Tag {
    T value;
};

using Types   = mpl::vector<std::string, double, int>;
using Tags    = mpl::transform<Types, Tag<mpl::_1> >::type;

using Variant = make_variant_over<Types>::type;
using AnyTag  = make_variant_over<Tags>::type;

namespace mydetail {
    struct assign_to : boost::static_visitor<bool> {
        template <typename V> bool operator()(Tag<V>& tagged, V const& value) const {
            tagged.value = value;
            return true;
        }

        template <typename T, typename V> bool operator()(T&&, V&&) const {
            return false;
        }
    };
}

bool setValue(AnyTag &tag, Variant const& val) {
    return boost::apply_visitor(mydetail::assign_to(), tag, val);
}

int main() {
    AnyTag t;

    t = Tag<std::string>();

    // corresponding type assigns and returns true:
    assert(setValue(t, "yes works"));

    // mismatch: no effect and returns false:
    assert(!setValue(t, 42));
    assert(!setValue(t, 3.1415926));
}

¹如果我正确理解了您的目标。我专注于“我想拥有的是setValue(AnyTag&tag,T&val),其中T是AnyTag的运行时类型。” 部分要求。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章