Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2001-09-30 07:11:23

> From: "Robin" <darkaurora_at_[hidden]>
>Sorry if this is a well discussed to death issue but why is
>lexical_cast taking a parameter by value and not const reference?
>Given that string types may be passed as the source, passing by value
>is not efficient.

Not efficient when compared with what? Don't forget that string objects
are cheap to copy in comparison to the initialisation and use of
stringstream objects. In this case, it is unlikely that you would detect
a significant difference between pass by copy and pass by reference.

>Is there any distinct reason to pass by value, other than following
>standard casting convention??

Originally it was passed by reference, but there was indeed a belief on
the list, IIRC, that it should follow the form of other custom keyword-
cast functions and be pass by copy. However, I am beginning to believe
that this was by false analogy, and const reference would in fact be
more suitable -- not for efficiency reasons (those are mostly mythical),
but for consistency with the built-in keyword casts and for generality,
ie working through abstract base classes. What do others think?

>Also, throwing an exception when a lexical_cast fails is not always
>ideal. Why not overload it to return a return code? Something like

As Daryle Walker pointed out, it would not then look like a cast.

>PS:I found a similiar discussion Nov 2000 but the issue seems
>unresolved. So what is the status?

The idea was kicked around a bit, but no suitable syntax could be found
without leading to an overly complex and gold-plated component. When you
start down that road to parameterisation you rapidly end up reinventing
stringstreams :->

Historically, no exception was thrown and the result in the event of
failure was the default constructed value type. This was more script-
like, but was felt to hush up potentially interesting failures when you
really did need to know.

Hope that clarifies things a little.


  Kevlin Henney phone: +44 117 942 2990
  mailto:kevlin_at_[hidden] mobile: +44 7801 073 508 fax: +44 870 052 2289
  Curbralan: Consultancy + Training + Development + Review

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