Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2004-01-24 08:03:49

OK here's my take on this:

1) An explicit constructor makes the user aware that they are doing
something potentially expensive: mainly this comes down to memory
allocations, but since these are an order of magnitude slower than
everything else they are important.

2) An explicit constructor can catch sloppy code:

std::string trim(const std::string & s)
return regex_replace(s, "\\A\\s+|\\s+\\z", ""); // inefficient use of
non-explicit constructor

In this case the code can be much more effieciently expressed as:

std::string trim(const std::string & s)
static const boost::regex e("\\A\\s+|\\s+\\z");
return regex_replace(s, e, "");

3) An explicit constructor can catch incorrect code:

std::string trim(const std::string & s)
return regex_replace("\\A\\s+|\\s+\\z", "", s); // oops

4) semantically a string is not the same thing as a regular expression:

What kind of regex does the string represent? POSIX extended? POSIX basic?
Perl? Is it case sensitive? and so on.

These all have sensible defaults, but hopefully if you are forced to look up
the regex constructor in the docs, then you will at least be aware of the
options available.

At present I do not believe that the case for removing the explicit overways
these arguments, but I accept that the issue may be finely balanced, and in
large part subjective.



Boost list run by bdawes at, gregod at, cpdaniel at, john at