Boost logo

Boost :

From: Jens Maurer (jmaurer_at_[hidden])
Date: 2000-05-28 16:17:08


Dave Abrahams wrote:
> on 5/26/00 5:00 PM, Jens Maurer wrote:
> > stream_cast is really useful.
>
> I think it's a really ingenious idea, but haven't yet found myself wanting
> to use it. Would you mind describing how you've used it in more detail? I
> would appreciate seeing the motivation.

Here are some code snippets, abstracted from the real stuff
to protect the innocent:

void do_test(int n, char *args[])
{
  // We don't want to ignore problems with the command line.
  // However, we don't want to make a big fuss out of it, either.
  unsigned int iterations = stream_cast_ex<unsigned int>(args[1]);

  for(int i = 0; i < iterations; ++i)
    // do something
}

double acos_ex(double x)
{
  // I like this "throw" one-liner with all relevant info
  if(x < -1 || x > 1)
    throw std::invalid_argument("acos with invalid argument: "
      + stream_cast_ex<std::string>(x) + " not in [-1,1]");
  // ...
}

int main(int argc, char * argv[])
{
  if(n != 2) { usage(); return 2; }
  try {
    do_test(argc, argv);
  } catch(std::exception & ex) {
     std::cout << "Exception: " << ex.what() << '\n';
     return 1;
  }
}


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