This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Should basic_*stream::is_open() be const?


On Fri, Aug 13, 2004 at 07:30:35PM +0200, Gabriel Dos Reis wrote:
> We seem to have diverged on the reality of the examples :-)

As a man of practise, I can assure you that no matter how
much one thinks that there is "no need" for something - there
ALWAYS is a need for it, by someone, somewhere.

Even the fact that this whole list cannot come up with an
example that satisfies you, should not be a reason to question
the correctness of this fix: it is almost an axioma for libraries
to assume there IS a need.

Personally, I find Nathan's example more than convincing.

class Entrophy {
private:
  std::vector<int> M_pool;
  // ...
public:
  // ...
  virtual bool external_entrophy() const = 0;
};

class EntrophyFile : public Entropy {
private:
  std::istream M_is;
  // ...
public:
  virtual bool external_entrophy() const { return M_is.is_open(); }
  // ...
};

In this a const Entropy makes sense, since you only read from it.
Yet, an implementation that reads external entropy from a stream
can only implement the virtual function as a const member function
and thus using a const istream.  Making external_entrophy non-const
might make sense from the point of view of the class that brings
in the istream (EntrophyFile), that is not something that the
base class can know about.  I am sure there would be other, even
better examples - I just can't think of them myself.

-- 
Carlo Wood <carlo@alinoe.com>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]