Boost logo

Boost :

From: Bronek Kozicki (Brok_at_[hidden])
Date: 2003-10-20 07:05:31


Pavol Droba <droba_at_[hidden]> wrote:
>> How about trim ( in_place( orig ) )?

> As I have already mentioned, inplace variant differ in the
> implementation from the copy variants. Such a convetion would be hard
> to achieve.

Not so, if in_place returns proxy object.

template <typename T> class inplace_proxy;
template <typename T> inplace_proxy<T&> in_place(T&);

template<> class inplace_proxy<std::string&>
{
public:
  inplace_proxy<std::string&> (std::string& str) : ref_(str) {}
  inplace_proxy<std::string&> (const inplace_proxy<std::string&> & src)
: ref_(src.ref_) {}
  std::string& ref() {return ref_;}
//...
private:
  std::string& ref_;
  inplace_proxy<std::string&>& operator=(const
inplace_proxy<std::string&>&);
};

template<>
inplace_proxy<std::string&> in_place<std::string>(std::string& str)
{
  return inplace_proxy<std::string&>(str);
}

(template "inplace_proxy<std::string&>" is just an idea, there might be
something else instead)

> IMHO copy and inplace variants should be very explicitly
> distinguished.

We might have two overloaded versions of mutating algorithms:

inplace_proxy<std::string&> trim(inplace_proxy<std::string&>);

std::string trim(const std::string &);

which will make both variants very easy to distinguish. Additional bonus
is that you can nest calls to both variants in very natural manner:

 ucase(trim(in_place(str1))); // in place
 std::string str3 = ucase(trim(str2)); // using temporary copies

There is a problem, though: proxy class (or template, as proposed above)
will add small overhead, which is problem for "in-place algorithms" as
these might be expected to be significaly faster than "copying
algorithms", thus carrying as small overhead as possible.

B.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk