c++ - operator== of a type erased container -


consider following class wraps container , type-erases type:

class c final {     struct b {         virtual bool empty() const noexcept = 0;     };      template<class t, class a>     struct d: public b {         // several constructors aimed         // correctly initialize underlying container          bool empty() const noexcept override { return v.empty(); }      private:         std::vector<t, a> v;     };      // ...  public:     //...      bool operator==(const c &other) const noexcept {         // ??         // compare underlying         // container of other.b 1         // of this->b     }  private:     // initialized somehow     b *b; }; 

i'd add operator== class c.
internally, should invoke same operator on underlying containers, i'm stuck on problem, don't know how that.
idea 2 instances of c equal if operator== of underlying containers return true.

whatever i've tried till now, ever ended being unable type of 1 of 2 underlying containers, 1 of other.
there easy solution can't see @ moment or should give up?

despite suggestion juanchopanza, found that, far underlying containers represent same concept (as example, different specializations of vector), maybe there no need of type-erased iterator.

below it's possible implementation relies on operator[] , size member method:

#include <vector> #include <cassert>  class clazz final {     struct basecontainer {         virtual std::size_t size() const noexcept = 0;         virtual int operator[](std::size_t) const = 0;         virtual void push_back(int) = 0;     };      template<class allocator>     struct container: public basecontainer {         container(allocator alloc): v{alloc} { }         std::size_t size() const noexcept override { return v.size(); }         int operator[](std::size_t pos) const override { return v[pos]; }         void push_back(int e) override { v.push_back(e); }      private:         std::vector<int, allocator> v;     };  public:     template<class allocator = std::allocator<int>>     clazz(const allocator &alloc = allocator{})         : container{new container<allocator>{alloc}} { }     ~clazz() { delete container; }      void push_back(int e) { container->push_back(e); }      bool operator==(const clazz &other) const noexcept {         const basecontainer &cont = *container;         const basecontainer &ocont = *(other.container);         bool ret = (cont.size() == ocont.size());          for(std::vector<int>::size_type = 0, s = cont.size(); < s && ret; i++) {             ret = (cont[i] == ocont[i]);         }          return ret;     }      bool operator!=(const clazz &other) const noexcept {         return !(*this == other);     }  private:     basecontainer *container; };  int main() {     clazz c1{}, c2{}, c3{};     c1.push_back(42);     c2.push_back(42);     assert(c1 == c2);     assert(c1 != c3); } 

open criticism, hoping answer can other users. :-)


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -