473,657 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rationale for 21.3.4p1

It was brought to my attention today that the Standard
(1998 version, anyway) says that if s is a const
std::string, then:
s[s.size()]

is well-defined and evaluates to 0.

Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementations in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.
Dec 11 '07 #1
8 1269
In article
<5a************ *************** *******@e23g200 0prf.googlegrou ps.com>,
Old Wolf <ol*****@inspir e.net.nzwrote:
It was brought to my attention today that the Standard
(1998 version, anyway) says that if s is a const
std::string, then:
s[s.size()]

is well-defined and evaluates to 0.

Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementations in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.
I hazard it's because that behavior makes std::string behave more or
less like a C string, and lets you iterate through the string, exiting
when operator[] returns 0.

-dr
Dec 11 '07 #2
On 11 Dec., 07:29, Dave Rahardja <moc.xo...@ajdr ahard.comwrote:
In article
<5a74d5ac-f5c0-47c4-b255-7e70fa7ab...@e2 3g2000prf.googl egroups.com>,
Old Wolf <oldw...@inspir e.net.nzwrote:
It was brought to my attention today that the Standard
(1998 version, anyway) says that if s is a const
std::string, then:
s[s.size()]
is well-defined and evaluates to 0.
Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementations in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.

I hazard it's because that behavior makes std::string behave more or
less like a C string, and lets you iterate through the string, exiting
when operator[] returns 0.
That can't be the reason as this requirement is for const std::string
only.
I reckon it has to do with some legacy support from before
standardization , but it is only a guess.
A good question from the old wolf!

/Peter
Dec 11 '07 #3
On Dec 11, 6:31 pm, "Alf P. Steinbach" <al...@start.no wrote:
Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementations in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.

In practice all implementations use one contiguous buffer which is also
the result of c_str().
Yes; but implementations need not actually put the 0 at
the end of the buffer until it's required, they could
behave just like every other library's counted string
in the meantime.
Dec 11 '07 #4
Alf P. Steinbach wrote:
* Old Wolf:
>On Dec 11, 6:31 pm, "Alf P. Steinbach" <al...@start.no wrote:
>>>Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementati ons in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.
In practice all implementations use one contiguous buffer which is also
the result of c_str().

Yes; but implementations need not actually put the 0 at
the end of the buffer until it's required, they could
behave just like every other library's counted string
in the meantime.

For a const string?

Well, it's not a convincing argument, just an idea as to rationale.
Keep in mind that non-const strings also have a const-member operator[] that
gets called when the string is referred to as const, e.g., passed to a
function that takes a const string & parameter. The standard requires that
operator[]( size() ) returns 0 in those cases.
Best

Kai-Uwe Bux
Dec 11 '07 #5
On Dec 11, 10:06 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Alf P. Steinbach wrote:
* Old Wolf:
On Dec 11, 6:31 pm, "Alf P. Steinbach" <al...@start.no wrote:
Why is this? It seems to me that it places undue
constraint on the implementation. I have looked at
implementatio ns in the past that just maintain a
counted string, and only append the \0 to the
buffer when c_str() is called. Such an implementation
would have to include overhead in its operator[] function
that would not be necessary without this clause.
In practice all implementations use one contiguous buffer which is also
the result of c_str().
Yes; but implementations need not actually put the 0 at
the end of the buffer until it's required, they could
behave just like every other library's counted string
in the meantime.
For a const string?
Well, it's not a convincing argument, just an idea as to rationale.
Keep in mind that non-const strings also have a const-member
operator[] that gets called when the string is referred to as
const, e.g., passed to a function that takes a const string &
parameter. The standard requires that operator[]( size() )
returns 0 in those cases.
I'm not sure how this interacts with the upcoming requirement
that the data in a string be contiguous, but at least at
present, the implementation of the const operator[] could be
something like:

CharT const&
basic_string< ... >::operator[]( size_t i ) const
{
static CharT const nul( 0 ) ;
return i < size() ? myRep[ i ] : nul ;
}

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 11 '07 #6
James Kanze <ja*********@gm ail.comwrote:
On Dec 11, 10:06 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Alf P. Steinbach wrote:
* Old Wolf:
>On Dec 11, 6:31 pm, "Alf P. Steinbach" <al...@start.no wrote:
>>>Why is this? It seems to me that it places undue
>>>constraint on the implementation. I have looked at
>>>implementati ons in the past that just maintain a
>>>counted string, and only append the \0 to the
>>>buffer when c_str() is called. Such an implementation
>>>would have to include overhead in its operator[] function
>>>that would not be necessary without this clause.
>>In practice all implementations use one contiguous buffer which is also
>>the result of c_str().
>Yes; but implementations need not actually put the 0 at
>the end of the buffer until it's required, they could
>behave just like every other library's counted string
>in the meantime.
For a const string?
Well, it's not a convincing argument, just an idea as to rationale.
Keep in mind that non-const strings also have a const-member
operator[] that gets called when the string is referred to as
const, e.g., passed to a function that takes a const string &
parameter. The standard requires that operator[]( size() )
returns 0 in those cases.

I'm not sure how this interacts with the upcoming requirement
that the data in a string be contiguous, but at least at
present, the implementation of the const operator[] could be
something like:

CharT const&
basic_string< ... >::operator[]( size_t i ) const
{
static CharT const nul( 0 ) ;
return i < size() ? myRep[ i ] : nul ;
}
Is string's iterator required to behave the same way?

void foo( const string& s ) {
string::const_i terator it = s.begin();
string::value_t ype c = it[s.size()]; // well defined?
}
Dec 11 '07 #7
On 2007-12-11 00:31:17 -0500, "Alf P. Steinbach" <al***@start.no said:
* Old Wolf:
>It was brought to my attention today that the Standard
(1998 version, anyway) says that if s is a const
std::string, then:
s[s.size()]

is well-defined and evaluates to 0.

Does it?

Wouldn't /really/ surprise me but I'm unfamiliar with that requirement.

It's not hard to find. [string.access]/1.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 11 '07 #8
On Dec 11, 1:16 pm, "Daniel T." <danie...@earth link.netwrote:
James Kanze <james.ka...@gm ail.comwrote:
On Dec 11, 10:06 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Alf P. Steinbach wrote:
* Old Wolf:
On Dec 11, 6:31 pm, "Alf P. Steinbach" <al...@start.no wrote:
>>Why is this? It seems to me that it places undue
>>constraint on the implementation. I have looked at
>>implementatio ns in the past that just maintain a
>>counted string, and only append the \0 to the
>>buffer when c_str() is called. Such an implementation
>>would have to include overhead in its operator[] function
>>that would not be necessary without this clause.
>In practice all implementations use one contiguous
>buffer which is also the result of c_str().
Yes; but implementations need not actually put the 0 at
the end of the buffer until it's required, they could
behave just like every other library's counted string
in the meantime.
For a const string?
Well, it's not a convincing argument, just an idea as to
rationale.
Keep in mind that non-const strings also have a const-member
operator[] that gets called when the string is referred to as
const, e.g., passed to a function that takes a const string &
parameter. The standard requires that operator[]( size() )
returns 0 in those cases.
I'm not sure how this interacts with the upcoming requirement
that the data in a string be contiguous, but at least at
present, the implementation of the const operator[] could be
something like:
CharT const&
basic_string< ... >::operator[]( size_t i ) const
{
static CharT const nul( 0 ) ;
return i < size() ? myRep[ i ] : nul ;
}
Is string's iterator required to behave the same way?
void foo( const string& s ) {
string::const_i terator it = s.begin();
string::value_t ype c = it[s.size()]; // well defined?
}
Good question. I don't think so. I can't find any text
requiring it, nor anything requiring s.begin()[i] to have the
same behavior as s[i] (although logically...).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Dec 12 '07 #9

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

Similar topics

24
2312
by: Matt Feinstein | last post by:
Hi all-- I'm new to Python, and was somewhat taken aback to discover that the core language lacks some basic numerical types (e.g., single-precision float, short integers). I realize that there are extensions that add these types-- But what's the rationale for leaving them out? Have I wandered into a zone in the space/time continuum where people never have to read binary data files? Matt Feinstein
0
1398
by: Alexander Grigoriev | last post by:
I hope Mr. Stroustrup can give an answer to this question: What was rationale behind the requirements to use an ampersand and a fully qualified name of a function, to form a pointer to a member function? Is there any syntax ambiguity without those? MS C++ compiler is quite happy when a plain function name is used (I haven't tried it in ANSI compliance mode, though), so for long time I've been pretty sure that it's OK...
2
2767
by: Howard Jess | last post by:
Given the html at the end of this message, I see how a DOM NodeList exhibits its "live" behavior; that is, adding elements to a document can change any NodeList variables, when there's *no* code that refers to them. I suppose I can imagine cases where this idea would be useful, but I can imagine many more where I'd like to get a *static* list of, e.g., the <p> elements in my document, and do something with them, regardless of any I...
8
1250
by: Robert Latest | last post by:
Hello, I'm new in javascript programming (but am quite literate in HTML, CSS, and C). It's amazing what one can do with JS and fairly modern browsers. One thing that struck me as odd was that there is no way to portably find out the size (in pixels) of the current browser window (or of any HTML element for that matter, unless it was inline-CSSed). I'm wondering what the reason for this might be.
52
3024
by: Christopher Benson-Manica | last post by:
gets() is universally acknowledged to be broken and useless; however, it is still part of the standard library. Why? Is there enough conforming code out there using gets() to justify retaining it? Are there plans to deprecate or eliminate it in a future version? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
4
1307
by: Bradford Chamberlain | last post by:
I've been curious for awhile about why C supports some of its binary operators in an op= format, but not others. For example, why are &= and |= supported by C, but not &&= and ||=? For awhile I was guessing that it was because the designers wanted to avoid 3-character operators, but then I remembered <<= and >>=. If anyone has any insight into the rationale for this, please let me know. My sense has been that such operators would...
17
11214
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
21
4586
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
15
4367
by: Ben Hinkle | last post by:
I'm curious, what was the rationale for making a builtin type _Bool but then having #define true 1 #define false 0 in stdbool.h? That seems very odd that true and false don't have type _Bool. In particular I'm poking around with some language extensions to C and one of the most obvious extensions is overloading. Since "true" doesn't have type _Bool it makes overloading behavior with _Bool very odd. You'd think that at least it could be...
1
1428
by: Nick Bastin | last post by:
I've done some searching in this newsgroup and on the internet and haven't found an answer, so I thought I'd ask here... :-) I don't understand why full specialization of a member function template in a class template is only valid if you fully specialize the class template as well. Consider the following example: template<class T, int ID> class RSMixin : public T
0
8310
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4155
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.