[Libpqxx-general] operator== for fields

Jeroen T. Vermeulen jtv at xs4all.nl
Tue Sep 25 15:56:27 UTC 2007


On Mon, September 24, 2007 23:06, Carlos Moreno wrote:

Hello Carlos,


> I would probably go as far as to suggest that class result::field
> should have implicit conversion to std::string --- as much as
> implicit conversion operators are widely seen as an almost
> unconditionally bad idea, I feel that in this case, result::field
> should almost be a typedef for std::string  (of course, that would
> mean sacrificing a lot of interesting functionality, so it is good
> as it is, being a class of its own).
>
> But intuitively, the data that we get from a database is a string;
> in the docs, I see some discussion about the issues of "intuition"
> often telling us that things would behave as they behave in the
> SQL realm  (since after all, what we're getting is a representation
> of data that lives in the database).

You raise an interesting issue.  I can see the usefulness of a conversion
to string, but I'm not sure I have a solid grasp of the risks yet:

i. Performance risks: we may be tempting the user to make repeated
implicit conversions when it might be better to keep a stringified field
value in a variable.  OTOH I've learned over time that initially libpqxx
went too far in protecting programmers from themselves. Maybe it's not
something I should worry about.

ii. Choice of string types: there are other standard string types besides
std::string, which I was hoping to support better in the future, and I
wonder if things might not get a bit messy if we start picking defaults.

iii. Mistaken conversions.  Are there any cases where you might
accidentally get conversions you don't want?

As you say, overloading operator== and operator!= for fields would satisfy
the most urgent needs, but it's not very complete.  It'd be a bit odd to
have some string operators work but not others, or to have comparisons to
some types but not to others.

I'll take an easy way out and ask: would there be anything actually wrong
with something like

std::string s(const pqxx::result::field &f)
{
  return f.as<std::string>();
}

and then accessing fields as

if (s(data[i][0]) == "foo")

?


Jeroen




More information about the Libpqxx-general mailing list