473,706 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why no non-integral static const initialiser's within class definition?

Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.

Is this correct and why is it so?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #1
14 2863

"Mike Hewson" <he******@optus net.com.au> wrote in message
Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.
The relevant clause is 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. In that case, the member can
appear in integral constant expressions within its scope. The member shall
still be defined in a namespace scope if it is used in the program and the
namespace scope definition shall not contain an initializer."

So the only relaxation is that for static const integral/enum types you can
get away with the definition if you don't use them in their program i.e.
don't treat them as l-values. The key point you are missing is that the
member can still appear in integral constant expressions (like case label).
So this clause is just a relaxation applicable only with integral constant
expressions.
Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed. Is this correct and why is it so?


No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;

Sharad

Jul 22 '05 #2
Mike Hewson wrote:
Have been researching as to why:
<example 1>
class ABC
{
static const float some_float = 3.3f;
};
<end example 1>

is not allowed.
Found that
<example 2>
class ABC
{
static const float some_float;
};
const float some_float = 3.3f;
should be:
const float ABC::some_float = 3.3f;
<end example 2>

is allowed.
Is this correct and why is it so?


The second example is correct.
To be able to do what you did in the first example you variable would
have to conform to the following set of requirements:
Must be static.
Must be const.
Must have some integral or enumeration type.
The initializing value must be an integral constant expression.
Can be initialized at most once. If you initialize them within the
class, you can't initialize them again in their definitions.

The floating point types violate the Must have some integral or
enumeration type requirement.

Jul 22 '05 #3
Sharad Kala wrote:
"Mike Hewson" <he******@optus net.com.au> wrote in message
The relevant clause is 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. In that case, the member can
appear in integral constant expressions within its scope. The member shall
still be defined in a namespace scope if it is used in the program and the
namespace scope definition shall not contain an initializer."
Yeah, I'd found that clause, but I don't read/see how it makes any
comment on non-integral types. Unless my comprehension co-processor is
down this week!

So the only relaxation is that for static const integral/enum types you can
get away with the definition if you don't use them in their program i.e.
don't treat them as l-values.
Yup, they're const....so can't be l-values, however I can surely 'use
them' in a way that doesn't assign to them ( otherwise what's the point
in creating it? )
The key point you are missing is that the
member can still appear in integral constant expressions (like case label).
So this clause is just a relaxation applicable only with integral constant
expressions.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.


Is this correct and why is it so?

No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;


I think

const float some_float = 3.3f;

is fine. ( Provided there is no other some_float about, in scope to
confuse things )

In any case, perhaps I should rephrase:

Why can I initialise *integral types* in this context and manner but not
any other fundamental type ( or 'user' defined )? Is it a compiler
implementation problem?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #4
ve*********@hot mail.com wrote:
Mike Hewson wrote:
Have been researching as to why:
<example 1>
class ABC
{
static const float some_float = 3.3f;
};
<end example 1>

is not allowed.
Found that
<example 2>
class ABC
{
static const float some_float;
};
const float some_float = 3.3f;

should be:
const float ABC::some_float = 3.3f;

<end example 2>

is allowed.
Is this correct and why is it so?

The second example is correct.
To be able to do what you did in the first example you variable would
have to conform to the following set of requirements:
Must be static.
Must be const.
Must have some integral or enumeration type.
The initializing value must be an integral constant expression.
Can be initialized at most once. If you initialize them within the
class, you can't initialize them again in their definitions.

The floating point types violate the Must have some integral or
enumeration type requirement.


Yup! I'm looking for the statement/source of that requirement.
Fair enough, does anyone know the rationale for that requirement?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #5
Mike Hewson wrote:
Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.
The declaration of a static data member is not a definition.
Therefore, it may not include an initializer (9.4.2#2).

As a special exception, a constant expression may be used as the
initializer of a const integral or const enumeration static data
member. But it's still not a definition, and a definition must be
provided somewhere (without an initializer), or its use will
invoke undefined behavior.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.

Is this correct and why is it so?


The above is allowed but doesn't do what you think. You need:

const float ABC::some_float = 3.3f;

HTH.
Jul 22 '05 #6

"Mike Hewson" <he******@optus net.com.au> wrote in message
Sharad Kala wrote:
"Mike Hewson" <he******@optus net.com.au> wrote in message
The relevant clause is 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. In that case, the member can
appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer."
Yeah, I'd found that clause, but I don't read/see how it makes any
comment on non-integral types. Unless my comprehension co-processor is
down this week!


It clearly states "const integral or const enumeration type". The point is
that const integral or const enumeration type could be used in integral
constant expressions which is not the case with non-integral constant
expressions.

So the only relaxation is that for static const integral/enum types you can get away with the definition if you don't use them in their program i.e.
don't treat them as l-values.


Yup, they're const....so can't be l-values, however I can surely 'use
them' in a way that doesn't assign to them ( otherwise what's the point
in creating it? )


Even if the program takes the address of the member it needs to be defined.
The key point you are missing is that the
member can still appear in integral constant expressions (like case label). So this clause is just a relaxation applicable only with integral constant expressions.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.


Is this correct and why is it so?

No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;


I think

const float some_float = 3.3f;

is fine. ( Provided there is no other some_float about, in scope to
confuse things )


Incorrect. You are just defining an independent global some_float this
way. To define the static member of class you have to write the way I wrote
in my last post.

In any case, perhaps I should rephrase:

Why can I initialise *integral types* in this context and manner but not
any other fundamental type ( or 'user' defined )? Is it a compiler
implementation problem?


Well, the proper place to ask is news:comp.std.c ++ where people can tell you
why only this is allowed in the language. My guess is that people did not
find it very useful to allow it with non-integral types. Of course this is
my interpretation, other poeple could enlighten us if there are any other
reasons too.

Sharad
Jul 22 '05 #7
Micah Cowan wrote:
Mike Hewson wrote:
Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.

The declaration of a static data member is not a definition. Therefore,
it may not include an initializer (9.4.2#2).


Aha! Outstanding!
For the group ( ISO/IEC 14882 ):

9.4.2 Static data members
2 The declaration of a static data member in its class definition is not
a definition and may be of an incomplete type other than cvqualified
void. The definition for a static data member shall appear in a
namespace scope enclosing the member’s class definition. In the
definition at namespace scope, the name of the static data member shall
be qualified by its class name using the :: operator. The initializer
expression in the definition of a static data member is in the scope of
its class (3.3.6).
As a special exception, a constant expression may be used as the
initializer of a const integral or const enumeration static data member.
But it's still not a definition, and a definition must be provided
somewhere (without an initializer), or its use will invoke undefined
behavior.
Oh, how I hate 'static' .... :-(

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.

Is this correct and why is it so?

The above is allowed but doesn't do what you think. You need:

const float ABC::some_float = 3.3f;


Yup, got that.

HTH.


You have, and thanks!

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #8

"Micah Cowan" <mi***@cowan.na me> wrote in message
news:41******** **************@ news.calweb.com ...
As a special exception, a constant expression may be used as the
initializer of a const integral or const enumeration static data
member. But it's still not a definition, and a definition must be
provided somewhere (without an initializer), or its use will
invoke undefined behavior.


Are you sure it invokes UB even if it is not used in the program ? Let me
quote Bill Gibbons (who wrote 9.4.2/4) - "It seems likely that the official
interpretation of the standard will be that the definition is needed only if
the static member is used as an lvalue, i.e. its address is taken".

Sharad
Jul 22 '05 #9
Sharad Kala wrote:
"Mike Hewson" <he******@optus net.com.au> wrote in message
Sharad Kala wrote:
"Mike Hewson" <he******@optus net.com.au> wrote in message
The relevant clause is 9.4.2/4
"If a static data member is of const integral or const enumeration type,
its
declaratio n in the class definition can specify a constant-initializer
which
shall be an integral constant expression. In that case, the member can
appear in integral constant expressions within its scope. The member
shall
still be defined in a namespace scope if it is used in the program and
the
namespace scope definition shall not contain an initializer."
Yeah, I'd found that clause, but I don't read/see how it makes any
comment on non-integral types. Unless my comprehension co-processor is
down this week!

It clearly states "const integral or const enumeration type". The point is
that const integral or const enumeration type could be used in integral
constant expressions which is not the case with non-integral constant
expressions.


I'd read that clause as "IF an X THEN do ...", that is it didn't ( alone
) comment about non-X's.

So the only relaxation is that for static const integral/enum types you
can
get away with the definition if you don't use them in their program i.e.
don't treat them as l-values.
Yup, they're const....so can't be l-values, however I can surely 'use
them' in a way that doesn't assign to them ( otherwise what's the point
in creating it? )

Even if the program takes the address of the member it needs to be defined.

The key point you are missing is that the
member can still appear in integral constant expressions (like case
label).
So this clause is just a relaxation applicable only with integral
constant
expression s.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.
Is this correct and why is it so?
No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;


I think

const float some_float = 3.3f;

is fine. ( Provided there is no other some_float about, in scope to
confuse things )

Incorrect. You are just defining an independent global some_float this
way. To define the static member of class you have to write the way I wrote
in my last post.


Yup, as per 9.4.2 #2
In any case, perhaps I should rephrase:

Why can I initialise *integral types* in this context and manner but not
any other fundamental type ( or 'user' defined )? Is it a compiler
implementatio n problem?

Well, the proper place to ask is news:comp.std.c ++ where people can tell you
why only this is allowed in the language.


Well it's a place. Good thought, will do. :-)
My guess is that people did not
find it very useful to allow it with non-integral types. Of course this is
my interpretation, other poeple could enlighten us if there are any other
reasons too.


I have this feeling that I'm missing/misunderstandin g something
important here. But if it's just arcana I'll relax. :-)

Thank you for your help, Sharad.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #10

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

Similar topics

12
4422
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
3
12251
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
1
6903
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all non-word characters from the string: // PCRE syntax: $string = preg_replace("/(+)/", "-", $string);
4
5305
by: bwmiller16 | last post by:
Guys - I'm doing a database consistency check for a client and I find that they're building unique indexes for performance/query reasons where they could be using non-unique indexes. Note that these columns in the unique indexes are truly unique and don't constitute a collision hazard of any kind. Now, I personally wouldn't use unique where non-unique would do but I
0
4489
by: Henry Wu | last post by:
Hi, I am aware that TransparencyKey only works with top-level forms or Non-MDI Child Forms, if one tries to set the opacity or transparencykey property to a MDI-Child form, it will have no effect. So, I tricked my Transparent Non-MDI Child form into my MDI-Container form via SetParent API as seen here in my VB.NET sample ( http://wuhenry.beigetower.org/position.zip ). I thought my problems are over, but apprently, even though I...
2
6112
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
0
2342
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
12830
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
9
3804
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
29896
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
8781
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9285
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9050
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
8993
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
7906
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
5938
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
4448
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
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2094
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.