473,583 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4495

"Jurko Gospodnetić" <ma*****@to.avo id.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
implementatio n 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
specification s 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**********@y ahoo.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
4200
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 including <map>. By linking my application and the lib, following errors occur: error LNK2005: "public: __thiscall std::basic_string<char,struct...
4
5303
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 a bool: class FakeBool { public: FakeBool() : b_(false) {} FakeBool(bool b) : b_(b) {} operator bool() { return b_; }
1
2181
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: alt.comp.lang.learn.c-c++ I'm trying to remove the \n from a string. If I just simply locate teh char and replace it with \0 then the destructor should only...
13
4595
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 which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the...
1
2579
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 even .length() functions, I get a segmentation fault. Is there a way to check what exists in 'abc' because it's definitely holding something .....
8
3722
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, ...) return (::memcmp(_First1, _First2, _Count));
8
14286
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 ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample....
5
3085
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 student, and trying my best. void retrieveImage(set<string>finalset, string hostname, string filename){ set<string>::iterator i; string img,...
0
1474
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) { return tolower (x) == tolower (y); } static bool lt (const char& x, const char& y) { return tolower (x) < tolower (y); }
0
7893
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6574
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.