473,513 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const static member

The following code produces strange errors in mingw.
Is a C++ problem or compiler problem?
----------------------------
#include <list>

class A
{
static const int B = 0;
std::list<intlst;
void calc();
};
void A::calc()
{
lst.push_back(B); // produces 'undefined reference to A::B'
int a = B;
lst.push_back(a); // its ...ok!
}

int main() { return 0; }
----------------------------
Nov 3 '08 #1
3 3084
On Nov 3, 3:34 pm, Chameleon <cham_...@hotmail.comwrote:
The following code produces strange errors in mingw.
Is a C++ problem or compiler problem?
----------------------------
#include <list>

class A
{
static const int B = 0;
Sadly, the above line does not define
storage for B, but only declares it.

You also need this defined somewhere:

const int A::B;

I believe it's just working for you in the
second case because it's getting optimized
away. Compiler bug, I guess.

Sean

Nov 3 '08 #2
Chameleon wrote:
The following code produces strange errors in mingw.
Is a C++ problem or compiler problem?
It is a problem with your code. In the C++98 specification all static
members of the class used in the program, have to be explicitly defined.
You failed to define the member, which is why from C++98 point of view
your code is ill-formed in _both_ contexts.

The revised C++ standard is more elaborate in this respect. Your
compiler's behavior is consistent with the revised specification.
----------------------------
#include <list>

class A
{
static const int B = 0;
std::list<intlst;
void calc();
};
void A::calc()
{
lst.push_back(B); // produces 'undefined reference to A::B'
'push_back' method accepts its parameters by constant reference and the
parameter and argument type matches exactly in this case, which means
that in the above context the reference is bound directly to the lvalue
'A::B'. This requires a definition of 'A::B' object. You forgot to
provide one. This is why you get an error.
int a = B;
lst.push_back(a); // its ...ok!
}
In this context the value of 'B' can be used as an rvalue, an integral
constant expression. There's no requirement to define 'A::B' for this
particular context.

--
Best regards,
Andrey Tarasevich
Nov 3 '08 #3
On Nov 3, 10:15*pm, sean_in_rale...@yahoo.com wrote:
On Nov 3, 3:34 pm, Chameleon <cham_...@hotmail.comwrote:
The following code produces strange errors in mingw. Is a
C++ problem or compiler problem?
Your problem. The code is not legal C++.
----------------------------
#include <list>
class A
{
* * static const int B = 0;
Sadly, the above line does not define storage for B, but only
declares it.
You also need this defined somewhere:
* const int A::B;
I believe it's just working for you in the second case because
it's getting optimized away. *Compiler bug, I guess.
No compiler bug. Not defining a variable which has been used is
undefined behavior, so anything the compiler does with it is
fine. In practice, given a declaration which can be used as an
integral constant expression, most compilers will only require
the definition if it is used in a context where there was not an
immediate lvalue to rvalue conversion. When you use it to
initialize another int, there is an immediate lvalue to rvalue
conversion; list<int>::push_back, on the other hand, takes a
reference, so there is no lvalue to rvalue conversion.

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

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

Similar topics

6
3470
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
3
3582
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
2
1747
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
14
2843
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
10
21309
by: Simon | last post by:
Hi, I have something like // // common.h const unsigned long m_dwStyle = 0x123; // // common.h
2
2230
by: Adrian | last post by:
Hi, In a header file I tried const char *someval="this is a test"; which is included all over the place and I get linker errors about multiple defines. Why is this not folded when const char...
5
11305
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
2
2307
by: mimi | last post by:
Hi,all. The section 13.5.1 of the <C++ primer 3rd editionsays, a static member frunction may not be declared as const or volatile. I could not explain to myself why? The constness seems to be...
6
3738
by: subramanian100in | last post by:
why can't a static member function be declared as const ? We can declare a non-static member function as const, to indicate that it does not modify the non-mutable data members. In the same way, is...
15
7839
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7264
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
7386
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
7543
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...
1
7106
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...
1
5094
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...
0
4749
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...
0
3236
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...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.