Hi all.
I was wondering. Can the standard basic_string<> c_str()
member function throw any exceptions? Is it perhaps
implementation dependent?
I tried checking the standard and as far as I can see it is
implementation dependent with no guarantees what so
ever, but I was hoping I may have missed something... :-)
When the C++ standard does not specify any exception
specifications for some function, nor does it list any
exceptions in the function description. Does that mean
that any exception may be thrown? If that is so, how are
we supposed to prepare for those exceptions in our code
other than 'something happened, I do not know what, but
it was not expected so I'm aborting...'.
Hope to hear from you soon.
Regards,
Jurko Gospodnetić 4 4473
"Jurko Gospodnetić" <ma*****@to.avoid.spam> skrev i en meddelelse
news:cu**********@ls219.htnet.hr... Hi all.
I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions? Is it perhaps implementation dependent?
It might - depending on the implementation. I tried checking the standard and as far as I can see it is implementation dependent with no guarantees what so ever, but I was hoping I may have missed something... :-)
When the C++ standard does not specify any exception specifications for some function, nor does it list any exceptions in the function description. Does that mean that any exception may be thrown? If that is so, how are we supposed to prepare for those exceptions in our code other than 'something happened, I do not know what, but it was not expected so I'm aborting...'.
You must react to the exception as you would in any other piece of code. The
most probable exception (i can't imagine other exceptions) should be out of
memory. Whether that exception came from basic_string::c_str or from any
other function probably would not matter to you. Hope to hear from you soon.
Regards, Jurko Gospodnetić
Kind regards
Peter
* Jurko Gospodnetić: I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions?
Yes, if defined so by the implementation.
Is it perhaps implementation dependent?
Yes.
I tried checking the standard and as far as I can see it is implementation dependent with no guarantees what so ever, but I was hoping I may have missed something... :-)
§17.4.4.8/3: "Any other function [than destructors] defined in the
C++ Standard Library that do not have an exception-specification may
throw implementation-defined exceptions unless otherwise specified",
with a note that says
"Library implementations are encouraged (but not required) to report
errors by throwing exceptions from (or derived from) the standard
exception classes".
When the C++ standard does not specify any exception specifications for some function, nor does it list any exceptions in the function description. Does that mean that any exception may be thrown?
If defined so by the implementation.
If that is so, how are we supposed to prepare for those exceptions in our code other than 'something happened, I do not know what, but it was not expected so I'm aborting...'.
char const* cStr( std::string const& s )
{
try
{
return s.c_str();
}
catch( std::exception const& )
{
throw;
}
catch( ... )
{
throw std::exception();
}
}
Of course, std::string is an ugly compromise that isn't really usable
for any of the purposes compromised on... Its only virtue is that it's
standard. And for that, and that only, we use it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: * Jurko Gospodnetić:
I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions?
Yes, if defined so by the implementation. Is it perhaps implementation dependent?
Yes. I tried checking the standard and as far as I can see it is implementation dependent with no guarantees what so ever, but I was hoping I may have missed something... :-)
§17.4.4.8/3: "Any other function [than destructors] defined in the C++ Standard Library that do not have an exception-specification may throw implementation-defined exceptions unless otherwise specified",
with a note that says
"Library implementations are encouraged (but not required) to report errors by throwing exceptions from (or derived from) the standard exception classes". When the C++ standard does not specify any exception specifications for some function, nor does it list any exceptions in the function description. Does that mean that any exception may be thrown?
If defined so by the implementation. If that is so, how are we supposed to prepare for those exceptions in our code other than 'something happened, I do not know what, but it was not expected so I'm aborting...'.
char const* cStr( std::string const& s ) { try { return s.c_str(); } catch( std::exception const& ) { throw; } catch( ... ) { throw std::exception(); } }
Of course, std::string is an ugly compromise that isn't really usable for any of the purposes compromised on... Its only virtue is that it's standard. And for that, and that only, we use it.
Bit of a minefield the stl if you use say a platform specific lock
mechanism which isn't an "auto mutex". One can't try every stl call just
incase.
"Spacen Jasset" <sp**********@yahoo.co.uk> skrev i en meddelelse
news:42******@x-privat.org...
[snip] Bit of a minefield the stl if you use say a platform specific lock mechanism which isn't an "auto mutex". One can't try every stl call just incase.
Well... the C++ way of doing things is not to use a "platform specific lock
mechanism", but wrap it so that locks will automatically be unlocked. Yopu
would not be able to do much realistic work with such a beast anyway.
/Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Florian Liefers |
last post by:
"Hello World\n",
i have the following problem:
One of my headerfiles for a lib is including <vector>.
When i compile the lib, everything is done well.
In my application another file is...
|
by: Jeff Paciga |
last post by:
I have been reading about the problems associated with vector<bool>.
Unfortunately, the usual work-arounds aren't viable for me, but I have
never seen anyone mention using a class that behaves like...
|
by: Billy N. Patton |
last post by:
-------- Original Message --------
Subject: <string>.replace
Date: Fri, 15 Oct 2004 11:07:19 -0500
From: Billy N. Patton <b-patton@ti.com>
Organization: Texas Instruments
Newsgroups:...
|
by: jstanforth |
last post by:
This is probably a very obvious question, but I'm not clear on what
operators need to be implemented for std::map.find() to work. For
example, I have a class MyString that wraps std::string, and...
|
by: mufasa |
last post by:
have a type given by:
typedef std::basic_string<wchar_t> mystringtype;
at one instacne of my program, i have a vairable called 'abc' of type
mystringtype. When i try to access the .c_str() or...
|
by: Earl Purple |
last post by:
On VC++.NET it is implemented like this
static int __cdecl compare
(
const _Elem *_First1,
const _Elem *_First2,
size_t _Count
)
{ // compare [_First1, _First1 + _Count) with [_First2, ...)...
|
by: davihigh |
last post by:
My Friends:
I am using std::ofstream (as well as ifstream), I hope that when i
wrote in some std::string(...) with locale, ofstream can convert to
UTF-8 encoding and save file to disk. So does...
|
by: KL |
last post by:
I have a problem trying to write to a binary file that I want to name
after a particular name from a set. My code for the function follows,
please excuse the horrible mistakes you may see, I am a...
|
by: pillbug |
last post by:
I am trying to convince the Greta regular expression library to use the
following char_traits:
struct ignore_case_traits : std::char_traits<char> {
static bool eq (const char& x, const char& y)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |