Topic: std::basic_string::operator const charT*()


Author: "John Hickin" <hickin@nortel.ca>
Date: 1997/10/02
Raw View
> This was intentional.  The designers decided it was too risky.  It would
>allow
>expensive conversions to happen when you didn't realize it.

I don't think that expensive conversions were the principle motivation for the
omission of the conversion operator. There is an excellent relevant discussion
in the Annotated C++ Reference Manual (the ARM), Chapter 12.2 Temporary
Objects.

While the rules for the destruction of temporary objects have changed since the
ARM was written I believe that the analysis presented in the ARM still applies:
"a temporary may sometimes be destroyed earlier than we would like". Given this
observation, it follows that a conversion operator which exports the internal
representation of an object is a dangerous thing to have.

--
John Hickin      Nortel Technology, Montreal, Quebec
(514) 765-7924   hickin@nortel.ca
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/09/27
Raw View
Edward Diener <eddielee@abraxis.com> writes:

> There is a c_str() member function that returns the const char *. I
> believe the explanation for not having a const char * casting operator
> is that since there is a const char * constructor the compiler would
> not know what to do when mixing the std:basic_string class with
> character constants in an expression since if you could convert both
> ways there would be ambiguity as far as which eay to convert and this
> would lead to a compiler error.

Another important motivation is to reduce the ease of shooting yourself
in the foot due to lifetime of temporaries issues.  For example:

 string foo();
 ...
 const char *s = foo();
 // oops, s is now a dangling pointer!

These problems can still arise with the c_str() function, but they're
much more visible than they would be with an implicit conversion.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Edward Diener <eddielee@abraxis.com>
Date: 1997/09/26
Raw View
There is a c_str() member function that returns the const char * . I believe the
explanation for not having a const char * casting operator is that since there is
a const char * constructor the compiler would not know what to do when mixing the
std:basic_string class with character constants in an expression since if you
could convert both ways there would be ambiguity as far as which eay to convert
and this would lead to a compiler error.

Ryszard Kabatek wrote:

> Hi,
> There is no const charT* casting operator defined for the
> std::basic_string class
> in the Draft.
> Why?
>
> --
> Ryszard Kabatek
> ---
> [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
> [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
> [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
Date: 1997/09/23
Raw View
Hi,
There is no const charT* casting operator defined for the
std::basic_string class
in the Draft.
Why?

--
Ryszard Kabatek
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]