Generic base class with multiple template specialized derived classes

前端 未结 4 996
梦如初夏
梦如初夏 2021-02-09 02:58

I have a finite amount of classes with the nearly-same implementation, the only different being the underlying type of data they manipulate:

class IntContainer
{         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 03:23

    This is a solution for any types of classes that can round-trip through a stringstream, and such conversion is the right way to convert between types. It isn't efficient at all:

    struct BaseContainer {
    protected:
      boost::any data;
      std::function< std::string( boost::any const& ) > toString;
      virtual void setDataAny( boost::any x, std::function< std::string( boost::any const& ) > convert ) {
        data = x;
        toString = convert;
      }
    public:
      virtual boost::any getDataAny() const {
        return data;
      }
      template
      void setData( T const& t ) {
        setDataAny( boost::any(t), []( boost::any const& a )->std::string {
          std::string retval;
          std::stringstream ss;
          try
          {
            ss << boost::any_cast< T >(a);
            ss >> retval;
            return retval;
          } catch(const boost::bad_any_cast &) {
            return retval;
          }
        });
      };
    template
    struct TypedContainer:BaseContainer {
    public:
      T getData() const {
        T retval;
        try {
          retval = boost::any_cast(getDataAny());
          return retval;
        } catch(const boost::bad_any_cast &) {
          std::string str = toString( getDataAny() );
          std::stringstream ss;
          ss << str;
          ss >> retval;
          return retval;
        }
      }
    };
    

    with fewer types, you could do something similar, so long as you have conversion functions between them.

    Alternatively, if you like exceptions, you could throw.

    Alternatively, you could use boost::variants, which do no conversions, but work from a finite list of types (they are basically tagged unions that support more types than C++03 lets union do, and with some nice semantics on assign/copy/etc).

提交回复
热议问题