Topic: Design issue / defect in class std::fpos ?


Author: Nikolai Pretzell <nikolai@sun.com>
Date: Wed, 27 Feb 2002 03:46:50 GMT
Raw View
Hi,

I've got the following question:

#### BEGIN citing char_traits.h of STLPORT 4.5

// Class fpos, which represents a position within a file.
// [ ... ]
template <class _StateT> class fpos
{
public:                         // From table 88 of the C++ standard.
 fpos(streamoff __pos) : _M_pos(__pos),
_M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}
 fpos() : _M_pos(0), _M_st(_STLP_NULL_CHAR_INIT(_StateT)) {}

 operator streamoff() const { return _M_pos; }

// [ ... ]

  fpos<_StateT> operator-(streamoff __off) {
 fpos<_StateT> __tmp(*this);
 __tmp -= __off;
 return __tmp;
 }
// [ ... ]
private:
 streamoff _M_pos;
 _StateT _M_st;
};

### END citing char_traits.h of STLPORT 4.5

'streamoff' is typedefed to 'long' or 'off64_t' or similar types.

IMHO that would look like an design error.
Defining both,
        operator  streamoff() const;
and
        operator-( streamoff);
leads to ambigouity, if the result of iostream::tellp() is used
in a variety of arithmetic expressions.

One example:

### BEGIN example
#include <strstream>
void ftest()
{
        std::strstream   s;

    s << "hello";

    if ( s.tellp() - 1 > 0 )    // This line doesn't compile.
        s << std::ends;
}
### END example

The error is: The compiler can't decide, if to use
a)   streamoff(s.tellp()) - 1
or
b)  s.tellp().operator-( streamoff(1) )


My questions to the experts:

1) Is that a design error?
2) If not, what is the intention behind that?
3) Is that behaviour standard-compliant?
4) Shouldn't be 'operator streamoff()' at least
   declared 'explicit', which would avoid the ambigouity with
   future compilers ;-) (Not all presently interpret 'explicit'.)
5) Shouldn't there better be completely thrown out
- either the cast operator streamoff()
- or both the arithmetic operators +() and -() ?


An additional question: Why aren't operator+() and -() const?


Thank you,

Nikolai


--
Nikolai Pretzell
Software Engineer Development Tools
Star Office Gmbh, Hamburg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]