How operator<< with boost::variant is implemented

时光毁灭记忆、已成空白 提交于 2019-12-10 16:09:44

问题


I understand that boost::variant is implemented something like so

template <typename... Vs>
struct variant {
    std::aligned_union<Vs...>::type buffer;
    ....
};

How can we make an operator<< for a struct like this that prints the casts the type stored in the buffer and passes that to operator<< for cout? For this we would need to know the type of the element stored in the buffer right? Is there a way to know this?

Also I am looking for an explanation of such an implementation, if one exists. Not just that it exists and how I can use it.


回答1:


Boost has an apply_visitor function, that takes a generic function object and passes the type of the variant into it. So implementing operator<< is as straightforward as:

template <class... Ts>
std::ostream& operator<<(std::ostream& os, boost::variant<Ts...> const& var) {
    return boost::apply_visitor(ostream_visitor{os}, var);
}

with:

struct ostream_visitor : boost::static_visitor<std::ostream&>
{
    std::ostream& os;

    template <class T>
    std::ostream& operator()(T const& val) {
        return os << val;
    }
};

Or simply:

template <class... Ts>
std::ostream& operator<<(std::ostream& os, boost::variant<Ts...> const& var) {
    return boost::apply_visitor([&os](const auto& val) -> std::ostream& {
        return os << val;
    }, var);
}

You can see some other examples in the tutorial.



来源:https://stackoverflow.com/questions/36223247/how-operator-with-boostvariant-is-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!