473,772 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Isn't 'std:.string::n pos' an integral constant?

Hi,

this
#include <string>
class test {
typedef std::string::si ze_type size_type;
static const size_type x = std::string::np os;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
'std::string::n pos' by '-1' it compiles.
Why isn't 'std::string;:: npos' a "known constant expression"? What
am I missing?

TIA;

Schobi
Sep 19 '08 #1
7 4268
Hendrik Schober wrote:
this
#include <string>
class test {
typedef std::string::si ze_type size_type;
static const size_type x = std::string::np os;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
'std::string::n pos' by '-1' it compiles.
Why isn't 'std::string;:: npos' a "known constant expression"? What
am I missing?
Hard to say. AFAICT, 14.7.1/1 requirements are met, the static data
member 'npos' should be instantiated (and the definition should exist),
and since 'npos' itself was initialised with a const expression, it is
allowed to be used in another const expression (5.19/1). Seems like a
bug in both compilers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 19 '08 #2
Hendrik Schober <sp******@gmx.d ekirjutas:
Hi,

this
#include <string>
class test {
typedef std::string::si ze_type size_type;
static const size_type x = std::string::np os;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
'std::string::n pos' by '-1' it compiles.
Why isn't 'std::string;:: npos' a "known constant expression"? What
am I missing?
Integral const expressions are usuful mostly for declaring C-style array
sizes and for template specialization. Most probably you never want to
declare an array of std::string::np os elements. OTOH, it may be imaginable
that you want to specialize a template with that value, but I guess this
was not considered a sufficiently sound reason to require std::string::np os
to be a const expression.

Paavo

Sep 22 '08 #3
James Kanze wrote:
On Sep 20, 8:56 pm, Hendrik Schober <spamt...@gmx.d ewrote:
[...]
> For one, there's 5.19 which says:
"An integral constant expression can involve [...]
static data members of integral or enumeration types
[...]."
So it seems that a static data member of integral type
is a constant integral expression.

Not necessarily. You cut part of the requirements. The
essential parts read "An integral constant expression can
involve [...]const variables or static data members of integral
or enumeration types initialized with constant
expressions,[...]" It's quite clear that the "or" between
"variables" and "static data members" joins just those two
nominal groups; that everything else (const, initialized with
constant expressions) applies to both. [...]
(Is it? Well, did I say I dread having to read the standard?)
> Then there's 9.4.2/4:
"If a static data member is of const integral or const
enumeration type, its declaration in the class
definition can specify a constant initializer which
shall be an integral constant expression (5.19)."
The way I read this, my 'test::x', which is of integral
type, can be initialized in-class, if I use a constant
integral expression.

It can be. Yes.
> Finally, 21.3 shows 'std::basic_str ing<T>::npos' as
static const size_type npos = -1;
(which is the only thing I was able to find resembling
a definition of what it should be).

And this is where it is unclear. How significant is the fact
that the value is given in this format? It is obvious that the
standard doesn't require textual identity with its class
definitions, but just what does it require? [...]
It doesn't? I would have thought it does.
> To me this looks like 'std::string::n pos' is defined as
being '-1', which would make it an integral constant
definition, which in turn means it could be used to
initialize constant static data member of integral type.
Which I thought I tried to do.
> I'm sure I missed something along the way (for example,
I have no idea what 14.7.1 has to do with all this), but
I wouldn't know what.

The real question is whether the initializer of npos is visible
when you want to use it as an integral constant expression. I
would sort of expect it to be in most implementations , because
this seems like the simplest way of doing it. But I'm far from
sure that it is required.
In VC9's implementation (Dinkumware), 'npos' is initialized
after the class template's definition. But so it was in VC71
(Dinkumware, too), where th same code used to compile...

Anyway, thanks for taking the time to explain.

Schobi
Sep 22 '08 #4
Paavo Helde wrote:
Hendrik Schober <sp******@gmx.d ekirjutas:
>Hi,

this
#include <string>
class test {
typedef std::string::si ze_type size_type;
static const size_type x = std::string::np os;
};
doesn't compile using either VC9 ("expected constant expression")
or Comeau Online ("constant value is not known"). If I replace
'std::string:: npos' by '-1' it compiles.
Why isn't 'std::string;:: npos' a "known constant expression"? What
am I missing?

Integral const expressions are usuful mostly for declaring C-style array
sizes and for template specialization. Most probably you never want to
declare an array of std::string::np os elements. OTOH, it may be imaginable
that you want to specialize a template with that value, but I guess this
was not considered a sufficiently sound reason to require std::string::np os
to be a const expression.
In my case it was some open source lib which, for reasons I haven't
looked into, introduced its own string class while depending on
'std::string' to supply its 'npos' value...
Paavo
Schobi
Sep 22 '08 #5
On Sep 22, 11:50 pm, Paavo Helde <nob...@ebi.eew rote:
Hendrik Schober <spamt...@gmx.d ekirjutas:
Interestingly, Comeau compile this
struct test1 {
static const int x = -1;
};
struct test2 {
static const int x = test1::x;
};
without any complaints. However, it compiles this
struct test1 {
static const int x;
};
const int test1::x = 1;
struct test2 {
static const int x = test1::x;
};
just as well.
Indeed. However, if test1 is a template, it fails. Seems a bit
inconsistent.
Not really. A template definition is not a variable definition;
only the instantiation of a template definition is a variable
definition. And the "point of instantiation" of a member
function or member static variable is immediately following the
namespace scope declaration which triggers the instantiation.
So if you write:

template< typename T >
struct test1
{
static int const x ;
} ;

template< typename T >
int const test1::x = 1 ;

struct test2
{
static int const x = test1< int >::x ;
} ;

, the compiler inserts the instantiation of the class test1<int>
immediately before the definition of test2, and the
instantiation of the static data member immediately after, i.e.:

struct test1< int >
{
static int const x ;
} ;

struct test2
{
static int const x = test1< int >::x ;
} ;
int const test1< int >::x = 1 ;

And the value of test1< int >::x isn't visible when test2::x is
declared.

--
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
Sep 23 '08 #6
On 2008-09-23 03:45:08 -0400, James Kanze <ja*********@gm ail.comsaid:
>
Out of curiousity, I generated the preprocessor output for a
program consisting of just an #include <string>, for all of the
compiler/library combinations I have available. None seem to
have an explicit specialization. They split with regards to
where npos is initialized, with those that initialize it in the
class never providing a declaration (which results in undefined
behavior if you use it in a context where the object is
required---but compilers can defined undefined behavior).
And the implementation of the standard library does not have to be
written in portable C++, something which even members of the standards
committee have to be reminded of.

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

Sep 23 '08 #7
On Sep 23, 2:20 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-09-23 03:45:08 -0400, James Kanze <james.ka...@gm ail.comsaid:
Out of curiousity, I generated the preprocessor output for a
program consisting of just an #include <string>, for all of
the compiler/library combinations I have available. None
seem to have an explicit specialization. They split with
regards to where npos is initialized, with those that
initialize it in the class never providing a declaration
(which results in undefined behavior if you use it in a
context where the object is required---but compilers can
defined undefined behavior).
And the implementation of the standard library does not have
to be written in portable C++, something which even members of
the standards committee have to be reminded of.
That was more or less what I meant to imply with my comment in
parentheses. In this case, the implementation of
std::basic_stri ng in the g++ library clearly contains undefined
behavior, according to the standard. But it works with g++,
because of what the compiler does in this particular case, and
that's all that matters; it's not an error in the library.

--
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
Sep 23 '08 #8

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

Similar topics

11
3096
by: Gnik | last post by:
I created a C# application with some large string constants and whenever I try to build the solution the compiler crashes with no error message. On further analysis the problem seems to occur when the string constant has around 2100 character. I tried changing the constant declaration to a string variable declaration and it works fine. Any ideas? The following code can be used to reproduce the error:
1
1535
by: Paul Steckler | last post by:
I'm using a managed C++ class that wants an unsigned char __gc as input. Is there a way to easily populate that array from a string constant (or wide string constant, etc.)? I'd like to do something like: unsigned char foo __gc = "your ad here"; but of course that doesn't work.
18
7339
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further processing: function saveText( scroll_list, t_area, listToBeUpdated ) { scroll_list.options.text = t_area.text; scroll_list.options.value= t_area.value; var req; var url = "http://mkmxg00/cgi/confirmUpload.pl";
2
16915
by: polilop | last post by:
When i open my page in IE it shows an error Unterminated string constant om Line..... When i look at the line it shows the line where the </SCRIPT> tag is ???? Moziila dose not see this error, allso avant browser (shell for IE) dosent see it. the script works fine, i'm just anoyed with IE calling errors. Anybody know why this is?
10
9468
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m < 4) str = "%01x"; else if (m < 9) str = "%02x"; else str = "%03x";
5
13741
by: ken s | last post by:
From server-side code I'm using Response.Write to display a javascript alert box. It works fine except when I try to include a new line character, which causes this javascript error: "Unterminated string constant" Here's my c# code: strText = "Line 1 text\nLine2 text"; Response.Write("<script>alert('" + strText + "');</script>");
8
2540
by: Marcus Kwok | last post by:
Is std::string::npos portably able to be incremented? For example, I want to insert some text into a string. If a certain character is found, I want to insert this text immediately after this character; otherwise I insert at the beginning of the string. On my implementation string::npos has the value of (string::size_type)-1, so incrementing it will make it 0, but can I rely on it? Also, say that the character occurs at the end of...
20
691
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above example will produce an error as we are trying to modify a constant. So, str1 is not a string constant in C++.
11
4282
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
0
10104
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
10038
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
9912
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
8934
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...
1
7460
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
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.