7

から派生したクラスをコーディングするために何時間も試みました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(); }    
};

(質問の上にある)基本的なスニペットコードは、さまざまなプラットフォームでテストされています

  • ブースト v1.33 + GCC 4.1 (Linux)
  • ブースト v1.52 + GCC 4.7 (MinGW)
  • ブースト v1.52 + Visual C++ 2010 (v10)

GCC の 2 つのバージョンのエラーの下に (誰かが気になる場合は、両方のいずれかを削除できます...)


$ 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_]
4

3 に答える 3

4

空の状態が欲しい

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

他の 2 つの基準では、クラスを使用する必要がありますが、継承は必要ありません。封じ込めが必要です。

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);
于 2012-12-06T20:51:10.557 に答える
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();
}
于 2012-12-10T08:46:57.670 に答える