如何实现boost :: variant派生类?

时间:2012-12-06 19:15:09

标签: c++ gcc boost derived-class boost-variant

我已经尝试了几个小时来编写一个派生自boost::variant的类。但我不明白是什么问题(我不明白编译错误意味着什么)。

实施干净的boost::variant派生类有哪些规则?

#include <boost/variant.hpp>

class MyVariant : public boost::variant<char,bool>
{
public:    
       MyVariant ()          : boost::variant<char,bool>( ) {}

       template <typename T>
       MyVariant(      T& v) : boost::variant<char,bool>(v) {}

       template <typename T>
       MyVariant(const T& v) : boost::variant<char,bool>(v) {}
};

int main ()
{
      MyVariant a;
      MyVariant b = a;        //compilation error
  //  MyVariant c = MyVariant();
  //  MyVariant d (true);
  //  MyVariant e ('E');
}

为什么我要使用继承?(编辑以向@zaufi提供更多详细信息)

  • 我想要一个空状态
  • 我想接受const char*作为string
  • 我想接受int作为long
  • 我想提供enum种类型

例如,在伪C ++代码中,我希望:

class MyVariant : public boost::variant<char,bool,long,std::string>
{
  typedef boost::variant<char,bool,long,std::string> super;

public:    

  // I know here I should specialize templeted constructors
  // but I is more clear like that, isn't it?    
  MyVariant()              : super('e')             {} //empty -> char
  MyVariant(char        c) : super(std::string(1,c)){} //char  -> string
  MyVariant(const char* s) : super(std::string(s) ) {} //char* -> string
  MyVariant(int         v) : super(long       (v) ) {} //TODO boundaries    
  /* other constructors ... */

  enum Type
  {
    NONE,  //my empty state = char type
    BOOL,
    LONG,
    STRING
  };

  Type type() const { return (Type) which(); }    
};

已在不同平台上测试了基本代码段(问题之上)

  • boost v1.33 + GCC 4.1(Linux)
  • 提升v1.52 + GCC 4.7(MinGW)
  • boost v1.52 + Visual C ++ 2010(v10)

低于我对GCC两个版本的错误 (如果它困扰某人,我可以删除其中一个......)


$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ myVariant.cpp
/usr/include/boost/variant/variant.hpp: In constructor 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::variant(T&) [with T = MyVariant, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]':
myVariant.cpp:10:   instantiated from 'MyVariant::MyVariant(T&) [with T = MyVariant]'
myVariant.cpp:19:   instantiated from here
/usr/include/boost/variant/variant.hpp:1348: error: call of overloaded 'convert_construct(MyVariant&, long int)' is ambiguous
/usr/include/boost/variant/variant.hpp:1262: note: candidates are: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(T&, int, mpl_::false_) [with T = MyVariant, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]
/usr/include/boost/variant/variant.hpp:1321: note:                 void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char, U1 = bool, U2 = boost::detail::variant::void_, U3 = boost::detail::variant::void_, U4 = boost::detail::variant::void_, U5 = boost::detail::variant::void_, U6 = boost::detail::variant::void_, U7 = boost::detail::variant::void_, U8 = boost::detail::variant::void_, U9 = boost::detail::variant::void_, U10 = boost::detail::variant::void_, U11 = boost::detail::variant::void_, U12 = boost::detail::variant::void_, U13 = boost::detail::variant::void_, U14 = boost::detail::variant::void_, U15 = boost::detail::variant::void_, U16 = boost::detail::variant::void_, U17 = boost::detail::variant::void_, U18 = boost::detail::variant::void_, U19 = boost::detail::variant::void_, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]
/usr/include/boost/variant/variant.hpp:1330: note:                 void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(const boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char, U1 = bool, U2 = boost::detail::variant::void_, U3 = boost::detail::variant::void_, U4 = boost::detail::variant::void_, U5 = boost::detail::variant::void_, U6 = boost::detail::variant::void_, U7 = boost::detail::variant::void_, U8 = boost::detail::variant::void_, U9 = boost::detail::variant::void_, U10 = boost::detail::variant::void_, U11 = boost::detail::variant::void_, U12 = boost::detail::variant::void_, U13 = boost::detail::variant::void_, U14 = boost::detail::variant::void_, U15 = boost::detail::variant::void_, U16 = boost::detail::variant::void_, U17 = boost::detail::variant::void_, U18 = boost::detail::variant::void_, U19 = boost::detail::variant::void_, T0_ = char, T1 = bool, T2 = boost::detail::variant::void_, T3 = boost::detail::variant::void_, T4 = boost::detail::variant::void_, T5 = boost::detail::variant::void_, T6 = boost::detail::variant::void_, T7 = boost::detail::variant::void_, T8 = boost::detail::variant::void_, T9 = boost::detail::variant::void_, T10 = boost::detail::variant::void_, T11 = boost::detail::variant::void_, T12 = boost::detail::variant::void_, T13 = boost::detail::variant::void_, T14 = boost::detail::variant::void_, T15 = boost::detail::variant::void_, T16 = boost::detail::variant::void_, T17 = boost::detail::variant::void_, T18 = boost::detail::variant::void_, T19 = boost::detail::variant::void_]

$ g++ --version
g++.exe (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ myVariant.cpp -I /c/.../include/
In file included from c:/.../include/boost/variant.hpp:17:0,
                 from myVariant.cpp:1:
c:/.../include/boost/variant/variant.hpp: In instantiation of 'boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::variant(T&) [with T = MyVariant; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]':
myVariant.cpp:9:66:   required from 'MyVariant::MyVariant(T&) [with T = MyVariant]'
myVariant.cpp:18:25:   required from here
c:/.../include/boost/variant/variant.hpp:1406:9: error: call of overloaded convert_construct(MyVariant&, long int)' is ambiguous
c:/.../include/boost/variant/variant.hpp:1406:9: note: candidates are:
c:/.../include/boost/variant/variant.hpp:1316:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(T&, int, mpl_::false_) [with T = MyVariant; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_; mpl_::false_ = mpl_::bool_<false>]
c:/.../include/boost/variant/variant.hpp:1376:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char; U1 = bool; U2 = boost::detail::variant::void_; U3 = boost::detail::variant::void_; U4 = boost::detail::variant::void_; U5 = boost::detail::variant::void_; U6 = boost::detail::variant::void_; U7 = boost::detail::variant::void_; U8 = boost::detail::variant::void_; U9 = boost::detail::variant::void_; U10 = boost::detail::variant::void_; U11 = boost::detail::variant::void_; U12 = boost::detail::variant::void_; U13 = boost::detail::variant::void_; U14 = boost::detail::variant::void_; U15 = boost::detail::variant::void_; U16 = boost::detail::variant::void_; U17 = boost::detail::variant::void_; U18 = boost::detail::variant::void_; U19 = boost::detail::variant::void_; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]
c:/.../include/boost/variant/variant.hpp:1385:10: note: void boost::variant<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::convert_construct(const boost::variant<U0, U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19>&, long int) [with U0 = char; U1 = bool; U2 = boost::detail::variant::void_; U3 = boost::detail::variant::void_; U4 = boost::detail::variant::void_; U5 = boost::detail::variant::void_; U6 = boost::detail::variant::void_; U7 = boost::detail::variant::void_; U8 = boost::detail::variant::void_; U9 = boost::detail::variant::void_; U10 = boost::detail::variant::void_; U11 = boost::detail::variant::void_; U12 = boost::detail::variant::void_; U13 = boost::detail::variant::void_; U14 = boost::detail::variant::void_; U15 = boost::detail::variant::void_; U16 = boost::detail::variant::void_; U17 = boost::detail::variant::void_; U18 = boost::detail::variant::void_; U19 = boost::detail::variant::void_; T0_ = char; T1 = bool; T2 = boost::detail::variant::void_; T3 = boost::detail::variant::void_; T4 = boost::detail::variant::void_; T5 = boost::detail::variant::void_; T6 = boost::detail::variant::void_; T7 = boost::detail::variant::void_; T8 = boost::detail::variant::void_; T9 = boost::detail::variant::void_; T10 = boost::detail::variant::void_; T11 = boost::detail::variant::void_; T12 = boost::detail::variant::void_; T13 = boost::detail::variant::void_; T14 = boost::detail::variant::void_; T15 = boost::detail::variant::void_; T16 = boost::detail::variant::void_; T17 = boost::detail::variant::void_; T18 = boost::detail::variant::void_; T19 = boost::detail::variant::void_]

3 个答案:

答案 0 :(得分:6)

接受的答案和其他答案都没有回答这个问题。有很好的理由从boost :: variant派生。

  • 您希望将方法添加到变体中,以使其行为更具多态性
  • 你想继续使用boost::apply_visitor(visitor(), variant)而不跳过篮球

以下是继承自MyVariant的{​​{1}}类型的工作示例。该代码要求C ++ 11继承boost::variant。(/ p>)的(复杂)构造函数

编辑: boost::variant中实施的等同比较等运算符不会自动为派生类工作。这是boost::variant如何实现的(非预期)结果,它明确地阻止了使用模板与“外来”类型的比较。可以使用显式重载启用运算符,如示例中所示。

boost::variant

答案 1 :(得分:5)

  

我想要一个空状态

boost::variant<boost::blank, bool, long, std::string>

有。那更容易。不需要杂乱的遗产。

  

我想提供枚举类型

enum Type
{
  NONE,
  BOOL,
  LONG,
  STRING
};

struct GetType : public boost::static_visitor<Type>
{
  Type operator()(boost::blank) {return NONE;}
  Type operator()(bool) {return BOOL;}
  Type operator()(long) {return LONG;}
  Type operator()(const std::string&) {return STRING;}
};

//Get the type
Type t = boost::apply_visitor(GetType(), theData);

这也很容易。另外,如果您向变体添加新类型,如果您不更新GetType以匹配,则代码将会中断。

另外两个条件要求您使用类,但不需要继承。你需要收容。

typedef boost::variant<boost::blank, std::string, long> VarType;

class MyVariant
{
public:
    //Default construction will initialize with boost::blank.
    MyVariant(char c) : m_var(std::string(1,c)) {}
    MyVariant(const char* s) : m_var(std::string(s)) {}
    MyVariant(int v) : m_var(long(v)) {}
    MyVariant(long v) : m_var(long(v)) {}

    VarType &operator *() {return m_var;}
    const VarType &operator *() const {return m_var;}

private:
    VarType m_var;
};

...

Type t = boost::apply_visitor(GetType(), *theData);

答案 2 :(得分:0)

最后我的实施基于 Nicol Bolas 的回答:

<强> Variant.h

class Variant 
{
  public:
  Variant()                     : v_(boost::blank()) {}
  Variant(bool     v)           : v_(v)              {}
  Variant(long     v)           : v_(v)              {}
  Variant(int      v)           : v_(long(v))        {}
  Variant(double   v)           : v_(v)              {}
  Variant(const std::string& s) : v_(s)              {}
  Variant(const char*        s) : v_(std::string(s)) {}

  typedef boost::variant <boost::blank, bool, 
                          long, double, std::string> bstvar;
  enum Type {                      
    NONE, BOOL,          // above underlying types
    LONG, DOUBLE, STRING // and enum must be consistent:
  };                     // (order must be same)

  operator          bstvar&  ()       { return v_; }
  operator    const bstvar&  () const { return v_; }
  bstvar      &     operator*()       { return v_; }
  bstvar const&     operator*() const { return v_; }
  bool              empty    () const { return  type() == NONE;          }
  bool              toBool   () const { return  boost::get<bool  > (v_); }
  long              toLong   () const { return  boost::get<long  > (v_); }
  double            toDouble () const { return  boost::get<double> (v_); }
  std::string const& toStr   () const { return  boost::get<std::string> (v_); }
  std::string      & toStr   ()       { return  boost::get<std::string> (v_); }
  inline Type        type    () const { return  (Type) v_.which();       }
  static Type        type    (const std::string&);
  static std::string type    (Type t);

  private:  bstvar v_;  // Data
};

<强> Variant.cpp

#include "Variant.h"
#include <sstream>
#include <algorithm> //std::transform

Variant::Type   Variant::type   (const std::string& str)
{
  std::string lower = str;
  std::transform (lower.begin(), lower.end(), lower.begin(), ::tolower);

  if (lower == "bool")     return BOOL;
  if (lower == "long")     return LONG;
  if (lower == "double")   return DOUBLE;
  if (lower == "string")   return STRING;
  else                     return NONE;
}

std::string  Variant::type  (Type t)
{
  switch (t)
  {
    case NONE:    return "none";
    case BOOL:    return "bool";
    case LONG:    return "long";
    case DOUBLE:  return "double";
    case STRING:  return "string";

    default:
      ;//see below
  }
  std::ostringstream oss;
  oss <<"Unexpected type="<< t;
  return oss.str();
}