Boost logo

Boost :

From: Noel Yap (Noel.Yap_at_[hidden])
Date: 2003-05-03 23:12:21


"Justin M. Lewis" wrote:
> A little bit of an overly simple example. How about something more like
>
> void MyGetCWD(c_out<std::string> cwd)
> {
> char *buf = new char[FIRSTSIZE];
> unsigned int size = FIRSTSIZE;
> while(getcwd(buf, sizeof(buf)) == NULL)
> {
> delete [] buf;
> size += step;
> buf = new char[size];
> }
>
> cwd = buf;
> delete[] buf;
> }

Why not:

std::string MyGetCWD()
{
  .
  .
  .

  return std::string( buf );
}

> Or, how about,
> void GetFileName(c_in_out<std::string> path, const std::string &filter)
> {
> std::string fname;
> FindFirstFileWithFilter(out(fname), path, filter);
> path += "/";
> path += fname;
> }

void GetFileName( ref< std::string > path, std::string const& filter )
{
  .
  .
  .
}

> I could probably make up much better examples, but I was doing something
> with getcwd on friday, so it's on my mind still.

I believe returning a string may be more optimal than passing in an out
parameter since:
- the compiler can optimize away constructor/destructor calls with RVO.
For example:

  std::string cwd( MyGetCWD() );

or what about usage like:

  std::cout << MyGetCWD() << std::endl; // only one std::string is
created

Noel


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