Uninformative error using boost :: iterator_adaptor

I am trying to wrap istreamusing boost progress_displayusing boost::iterator_adaptor. What I wrote

class ifstreamWithProgress: public boost::iterator_adaptor <
  ifstreamWithProgress,
  char*,
  boost::use_default,
    boost::forward_traversal_tag > {
public:
//  ifstreamWithProgress(const char* fname_) {}
  ifstreamWithProgress(): iter(std::istream_iterator<char>()), pd(0)
  {}

  ifstreamWithProgress(const std::string& fname_): fname(fname_), fsize(0), pd(fsize) {
    std::ifstream file(fname.c_str(), std::ios::binary);
    fsize = file.tellg();
    file.seekg(0, std::ios::end);
    fsize = file.tellg() -  fsize;
    file.seekg(0, std::ios::beg);
    iter = std::istream_iterator<char>(file);
    pd.restart(fsize);
  }

  ~ifstreamWithProgress() {
    while( ++pd < fsize);
  }

  const std::istream_iterator<char> getRawIstream() const {
    return iter;
  }

private:
  std::string fname;
  friend class boost::iterator_core_access;
  std::istream_iterator<char> iter;
  std::streampos fsize;
  progress_display pd;

  void increments() {
    iter++;
    ++pd;
  }

  bool equal(const ifstreamWithProgress& rhs) const {
    return this->iter == rhs.getRawIstream();
  }
};

It compiles. However, when I started doing something like

  ifstreamWithProgress is("data.txt");
  ifstreamWithProgress eos;
  is != eos;

I get a compile-time error saying that it is not being copied. This makes sense because the mapping class is derived from boost::noncopyable. However, I do not understand where the copying takes place. Any pointers?

PS: error message

1>c:\users\leon.sit\documents\myprojects\c++\general_models\phoenixdsm\phx\fileProgressBarWrapper.hpp(58) : error C2248: 'boost::noncopyable_::noncopyable::noncopyable' : cannot access private member declared in class 'boost::noncopyable_::noncopyable'
1>        C:\Program Files\boost\boost_1_44\boost/noncopyable.hpp(27) : see declaration of 'boost::noncopyable_::noncopyable::noncopyable'
1>        C:\Program Files\boost\boost_1_44\boost/noncopyable.hpp(22) : see declaration of 'boost::noncopyable_::noncopyable'
1>        This diagnostic occurred in the compiler generated function 'ifstreamWithProgress::ifstreamWithProgress(const ifstreamWithProgress &)'

which does not indicate anywhere in the source. However, it compiles after commenting out the comparison string.

+3
source share
2 answers

, . , . progress_display, , .

, progress_display = . :

class ifstreamWithProgress: public boost::iterator_adaptor <
  ifstreamWithProgress,
  char*,
  boost::use_default,
    boost::forward_traversal_tag > {
public:
  ifstreamWithProgress() : pd(0) {
    pd = new progress_display(...);
  }

  ifstreamWithProgress::ifstreamWithProgress(const ifstreamWithProgress &r) : pd(0) {
    pd = new progress_display(...);
  }

  ~ifstreamWithProgress() {
    if (0 != pd) {
      delete pd;
    }
  }

  ifstreamWithProgress& operator= (const ifstreamWithProgress &r) {
    if (0 != pd) {
      delete pd;
    }
    pd = new progress_display(...);
    return *this;
  }

private:
  progress_display *pd;
};

shared_ptr<progress_display>.

+1

, . , , . operator !=, boost_adaptor. :

is != eos;

:

bool operator != (ifstreamWithProgress, ifstreamWithProgress);

( ). ifstreamWithProgress, , , , .

+1

Source: https://habr.com/ru/post/1780154/


All Articles