[Libpqxx-general] Arguable defect (relatively minor)

Richard B. Kreckel kreckel at ginac.de
Sat Feb 2 19:19:33 UTC 2008


Hi!

Carlos Moreno wrote:
> What I claim that is (arguably) a defect in libpqxx is defining
> operator<< as a template with STREAM being a type parameter.
> 
> If it was specifically defined with std::ostream, as opposed to a
> template parameter to accept any type, it would allow interaction
> with other classes that are "pseudo output streams", like the above
> class X.
> 
> Notice that my argument is that there is no good reason for the
> operator<< of libpqxx to interact with anything other than a
> *real* std::ostream, since it's not libpqxx the one providing
> the generic stream functionality, but simply providing the
> "connector" from your class field to something that is an
> ostream.
> 
> If someone wants to provide their "pseudo output stream", then
> it really is their responsibility to do tricks like the above
> template <typename T> operator<< (X, T).  If not, it is because
> they're dealing only with data types for which they know how
> to serialize and they have access to the internals.  It could not
> be the case that they would successfully interact with
> pqxx::result::field --- the only way is if everyone sticks to
> the real std::ostream functionality.
> 
> Makes sense?

I am only beginning to dig into libpqxx, but I think you are right. I've 
never before seen the stream type being templateized in an operator<< 
and I doubt it is useful.

The comment in result.hxx suggests that the intent is to allow things 
like ostringstreams. But these are ostreams anyway, after all. Maybe, 
one will have to allow for stream template parameters? If so, it would 
be more appropiate to directly templateize them, like so:

Index: result.hxx
===================================================================
--- result.hxx  (revision 1289)
+++ result.hxx  (working copy)
@@ -948,8 +948,8 @@
   * S >> L;
   * @endcode
   */
-template<typename STREAM>
-inline STREAM &operator<<(STREAM &S, const pqxx::result::field &F) 
//[t46]
+template<typename CharT>
+inline PGSTD::basic_ostream<CharT> 
&operator<<(PGSTD::basic_ostream<CharT> &S, const pqxx::result::field 
&F)   //[t46]
  {
    S.write(F.c_str(), F.size());
    return S;

This seems to work fine.

Cheers
   -richy.
-- 
Richard B. Kreckel
<http://www.ginac.de/~kreckel/>


More information about the Libpqxx-general mailing list