c++ - 如何在 C++ 中实现基本的 Variant(以及 Variant 上的访问者)模板?

标签 c++ templates variant typelist

我尝试阅读:

http://www.boost.org/doc/libs/1_41_0/boost/variant.hpp


http://www.codeproject.com/KB/cpp/TTLTyplist.aspx


and chapter 3 of "Modern C++ Design"

但仍然不明白变体是如何实现的。任何人都可以粘贴一个简短的示例来说明如何定义类似的内容:

class Foo {
  void process(Type1) { ... };
  void process(Type2) { ... };
};


Variant<Type1, Type2> v;

v.somethingToSetupType1 ...;

somethingToTrigger process(Type1);

v.somethingToSetupType2 ...;

somethingToTrigger process(Type2);

谢谢!

最佳答案

如果我必须定义一个变体对象,我可能会从以下开始:

template<typename Type1, typename Type2>
class VariantVisitor;

template<typename Type1, typename Type2>
class Variant
{
public:
   friend class VariantVisitor<Type1, Type2>;
   Variant();
   Variant(Type1);
   Variant(Type2);
   // + appropriate operators =
   ~Variant(); // deal with memory management

private:
    int type; // 0 for invalid data, 1 for Type1, 2 for Type2
    void* data;
};

template<typename Visitor, typename Type1, typename Type2>
class VariantVisitor 
{
   private:
     Visitor _customVisitor;
   public:
   void doVisit(Variant<Type1, Type2>& v)
   {
      if( v.type == 1 )
      {
          _customVisitor( *(Type1*)(v.data));
      }
      else if( v.type == 2 )
      {
          _customVisitor( *(Type2*)(v.data));
      }
      else
      {
         // deal with empty variant
      }
   }
};
template<typename Visitor, typename Type1, typename Type2>
void visit( Visitor visitor, Variant<Type1, Type2> v )
{
  VariantVisitor<Visitor, Type1, Type2>(visitor).doVisit(v);
}

然后使用 MPL vectors使该方法不仅仅适用于两种不同的类型。

最后,你可以写这样的东西:

Variant<Type1, Type2> v;
class MyVisitor
{
  public:
  operator()(Type1);
  operator()(Type2);
};

MyVisitor visitor;
v = Type1();
visit(visitor, v);
v = Type2();
visit(visitor, v);

注意:这段代码不可能编译,但这描述了我使用的想法。

关于c++ - 如何在 C++ 中实现基本的 Variant(以及 Variant 上的访问者)模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2131604/

相关文章:

c++ - const string a = "tmp"vs const string& a = "tmp"

c++ - 专门化非模板类的模板方法 - 在 MSVC 中工作,在 GCC 中编译错误

c++ - 如何避免虚拟继承

c++ - std::variant 使用整数数组中的元素作为 std::variant 中的目标类型从 std::variant 获取内容

在 OCaml 中打印变量类型

c++ - GCC9 是否允许避免 std::variant 的无值状态?

c++ - 使用 ifstream 加载文件时出错 - 只加载了一小部分

c++ - std::map::iterator 引用不存在的对象?

c++ - 裸指针到底有多糟糕?

c++ - 模板类实例接收不同类型的同一模板类的另一个实例