Topic: abstract class as first template argument to ostream_iterator permissible?
Author: richtesi@informatik.tu-muenchen.de (Simon Richter)
Date: Wed, 1 Sep 2004 23:50:45 GMT Raw View
Hi,
In a project, I have an abstract base and several implementing classes, plus
an operator<< to append such an object to an ostream, and an ostream_iterator
on top of that:
class Abstract {
public:
virtual ~Abstract(void);
virtual std::ostream &writeTo(std::ostream &) const = 0;
};
class Impl1 : public Abstract {
public:
virtual ~Impl1(void);
virtual std::ostream &writeTo(std::ostream &) const;
};
std::ostream &operator<<(std::ostream &s, const Abstract &a) {
return a.writeTo(s);
}
int main(int, char **) {
Impl1 i;
std::ostream_iterator<Abstract> o(std::cout);
*o++ = i;
return 0;
}
g++ 3.4 accepts this code, Sun CC does not, telling me that Abstract is an
abstract class (RogueWave STL obviously instantiates such an object obviously).
The C++ standard does not specify whether the first template argument to
std::ostream_iterator needs to be a concrete class.
It is obvious that it is possible to provide an implementation that accepts an
abstract class here. Should this be made a requirement, so that programmers can
use ostream_iterators with abstract base classes, or is it better to leave that
"implementation defined", effectively disallowing its use in portable code?
Simon
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]