473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Can basic_string<>::c_str() throw exceptions?

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ć
Jul 23 '05 #1
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
Jul 23 '05 #2
* 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?
Jul 23 '05 #3
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.

Jul 23 '05 #4

"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
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
4
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...
1
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:...
13
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...
1
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...
8
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, ...)...
8
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...
5
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...
0
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.