【问题标题】:Can boost variants safely be used with pointers to forward declared classes?boost 变体可以安全地与指向前向声明类的指针一起使用吗?
【发布时间】:2021-06-26 15:54:19
【问题描述】:

boost 变体能否安全地接受指向前向声明的类的指针,而不会产生任何意外影响,例如将它们与 visitors 一起使用?

class A;
class B;

typedef boost::variant<A*, B*> Variant;

class A {
public:
    A() {}
};

class B {
public:
    B() {}
};


【问题讨论】:

  • 是什么阻止你尝试编译?
  • @S.M.它编译得很好,但我想知道在变体中使用指针是否是个好主意
  • 您需要解释一下“安全”是什么意思。 “Safely accept”看起来编译成功了。
  • @S.M.虽然我同意这个问题过于抽象,但我认为我可以感觉到需要出现的通常应用。所以,我添加了我的答案。

标签: c++ boost boost-variant


【解决方案1】:

我建议使用内置的递归元素支持来实现这个确切的目的。它使(取消)分配自动和异常安全。

这是一个完整的演示,其中B 实际上递归包含vector&lt;Variant&gt;(这是前向声明元素类型的用例的 90%):

Live On Coliru

#include <boost/variant.hpp>
#include <iostream>
#include <iomanip>
struct A;
struct B;

typedef boost::variant<A, B> Variant;

struct A {
    int solution = 42;
};

struct B {
    std::string answer = "Thanks for all the fish!";
    std::vector<Variant> other { A{1}, A{2}, B{"Three", {}}, A{4} };
};

struct Visitor {
    std::string indent = " - ";
    void operator()(Variant const& v) const {
        boost::apply_visitor(Visitor{"  " + indent}, v);
    }
    void operator()(A const& a) const { std::cout << indent << a.solution << "\n"; };
    void operator()(B const& b) const {
        std::cout << indent << std::quoted(b.answer) << "\n";
        for (auto& v : b.other) {
            operator()(v);
        }
    };
};

int main()
{
    Variant v;
    v = A{};

    boost::apply_visitor(Visitor{}, v);

    v = B{};
    boost::apply_visitor(Visitor{}, v);
}

打印

 - 42
 - "Thanks for all the fish!"
   - 1
   - 2
   - "Three"
   - 4

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2016-01-17
相关资源
最近更新 更多