473,412 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Is this a bug of the C++ compiler of VS 2005?

See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;
};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj
According to the C++ standard 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."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?

Jun 6 '07 #1
12 1331
Lighter wrote:
See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;
};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj
According to the C++ standard 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."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?
Looks like a defect in their linker. Post to the VC++ newsgroup and/or
submit a bug report to Microsoft.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '07 #2
This is standard C++ behavior.

Just remove the "= 9" from your struct definition and it will work the
way you would expect.
On Jun 6, 9:47 am, Lighter <cqu...@gmail.comwrote:
See an example at first:

hdr.h
--------------

struct X
{
static const int m = 9;

};

=====================

source1.cpp
---------------

#include "hdr.h"

const int X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

link error:
source1.obj : error LNK2005: "public: static int const X::m" (?
m@X@@2HB) already defined in main.obj

According to the C++ standard 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."

Judging from the rule above, my code is correct. However, if I
commented the statement: const int X::m; then everything is ok. Why?

Jun 6 '07 #3
witkamp wrote:
This is standard C++ behavior.

Just remove the "= 9" from your struct definition and it will work the
way you would expect.
lol. Of course, removing a piece the program solves the problem :)

But have you read the message? Why should him remove =9 if he wants his
const static int member to be initialized in the class definition?
He also cited the standard, so, if you do not agree with something,
please provide an explanation.

Regards,

Zeppe
Jun 6 '07 #4
On Wed, 6 Jun 2007 13:13:07 -0400, Victor Bazarov wrote:
>Looks like a defect in their linker. Post to the VC++ newsgroup and/or
submit a bug report to Microsoft.
I don't know if it has ever been reported but it is an old bug, dating
back at least to VC 7.1. It is affected by the /Za flag, BTW: usually
/Za will make everything link, but then the resulting exe might not
work at all (great, isn't it? 98% conformity). This happens for
instance in the unit tests for
<http://breeze.svn.sourceforge.net/viewvc/breeze/trunk/breeze/meta/maximum.hpp>

but also in much simpler cases.

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 6 '07 #5
Visual C++ is not an iso-compliant compiler ...
(no matter which version)
Jun 7 '07 #6
Sascha Bohnenkamp wrote:
Visual C++ is not an iso-compliant compiler ...
(no matter which version)
None of the current compilers is an iso-compliant compiler. Visual C++
is a good compiler, if you have some specific issue we can discuss them,
like in the OP post, but please, refrain from trolling.

Regards,

Zeppe

Jun 7 '07 #7
On 6 kesä, 19:47, Lighter <cqu...@gmail.comwrote:
However, if I
commented the statement: const int X::m; then everything is ok. Why?
Because m is defined in the struct already. You don't need
another definition.

Jun 7 '07 #8
Krice wrote:
On 6 kesä, 19:47, Lighter <cqu...@gmail.comwrote:
>However, if I
commented the statement: const int X::m; then everything is ok. Why?

Because m is defined in the struct already. You don't need
another definition.
c'mon guys. Let's read the messages before answering ;)

Regards,

Zeppe
Jun 7 '07 #9
Krice wrote:
: On 6 kesä, 19:47, Lighter <cqu...@gmail.comwrote:
:: However, if I
:: commented the statement: const int X::m; then everything is ok.
:: Why?
:
: Because m is defined in the struct already. You don't need
: another definition.

You do actually, but sometimes you get away with it.

Try passing the value by const reference, like to std::max(), and the
linker will now complain that it cannot find the definition.
Bo Persson
Jun 7 '07 #10
On 7 Jun, 11:49, Krice <pau...@mbnet.fiwrote:
On 6 kesä, 19:47, Lighter <cqu...@gmail.comwrote:
However, if I
commented the statement: const int X::m; then everything is ok. Why?

Because m is defined in the struct already.
No it's not. It's declared and given an initialiser.
You don't need
another definition.
The OP's code doesn't have "another" definition, it has exactly one
definition.

Reread the section of the standard that the OP quoted.

Gavin Deane

Jun 7 '07 #11
On Jun 6, 2:23 pm, witkamp <theodore.witk...@gmail.comwrote:
This is standard C++ behavior.
No, it is not. As quoted by the colleague, the Standard allows the
initialization inside the class declaration, but one still needs to
define the variable outside. What the Visual Studio does is to allow
the declaration without definition, defining it automatically. When
one defines the same variable, the VS "help" ends messing up the code.

You're right, Lighter, the VS behavior is error prone regard to the
Standard Spec.

Wanderley Caloni
======================
http://www.cthings.org

Jun 8 '07 #12
ppl
On Jun 6, 1:47 pm, Lighter <cqu...@gmail.comwrote:
See an example at first:

hdr.h
--------------

struct X
{
staticconstint m = 9;

};

=====================

source1.cpp
---------------

#include "hdr.h"

constint X::m;

=====================

main.cpp
---------------

#include "hdr.h"

int main()
{}

=====================

linkerror:
source1.obj : error LNK2005: "public:staticintconstX::m" (?
m@X@@2HB) already defined in main.obj

According to the C++ standard 9.4.2/4
"If astaticdata member is ofconstintegralorconstenumeration
type, its
declaration in the class definition can specify a constant-initializer
which
shall be anintegralconstant expression. In that case, the member
can
appear inintegralconstant 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."

Judging from the rule above, my code is correct. However, if I
commented the statement:constint X::m; then everything is ok. Why?
I did observe the same problem in VC++2003 recently. I've read the
same paragraph of the standard and came to the same conclusion.

This problem can become tricky when you are also dealing with g++. The
GNU compiler properly implement the standard (afaik) and will yield a
linking error if you do not properly define m in a translation unit.
However, if you omit m definition, it may not yield a linking error in
some cases where, I assume, m references are inlined.

An example of this is when you use m directly (e.g. X::m) it may be
inlined and may not yield a linking error. Otoh, if you access m
indirectly via a non-inline method, g++ will generate the linking
error if you omit m definition.

See also http://docs.freebsd.org/info/g++FAQ/...a_members.html

We resolved to simply remove the initializer in the declaration and
place it in the definition. This will work with g++ and fix the issue
with VC++2003/2005.

Please inform me if someone already wrote a bug report to Microsoft
and/or know of an acknowledgment of this problem on Microsoft website.

Thank you,
ppl

Jun 16 '07 #13

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

Similar topics

18
by: Carramba | last post by:
Hi! I am wondering what is the best way to control that compiler manage variable names longer then 8 signs. Does it enought to set 2 variables with the same name and difference after 8 sign and...
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
6
by: Russ | last post by:
This really is the worst Microsoft product I have ever had the misfortune to work with. After the excellent feattures and stability of VS.NET 2002 and 2003, I was confident enough to start a...
1
by: Bruce | last post by:
We suddenly got a mysterious error message on our Windows Server 2003 (SP1) after rolling out a bug fix on a web services application this morning. The application compiles and runs fine on the...
4
by: sqlguy | last post by:
Why do we have to contact MS for a problem that has been with this compiler from at least the beta of VS 20005. I am so sick and tired of the 30 - 40 clicks it takes to dismiss VS when there is a...
0
by: gmsieling | last post by:
We're getting compiler error CS0006, with increasing frequency. It says that a randomly named DLL was not found. This happens at random intervals when you try to run the application. We're using the...
8
by: STG | last post by:
Greetings, My group has an SDK that was developed 5 years ago with VC++ 6. Over the last years, the requests for a VS.NET SDK has reached critical mass and I am now in the process of doing that....
4
by: Break2 | last post by:
I am new to C++ and am currently using Visual Studio 2005 after having first tried Borland C++ Builder 6.0 and CodeGear C++ Builder 2007. The thing I like about Visual Studio 2005 is the 'step over'...
0
by: Norman Diamond | last post by:
Does anyone know how to add the VC++2005 compiler for x64 to an existing installation of Visual Studio 2005 SP1? I opened Control Panel - Program Additions and Deletions - Microsoft Visual...
2
by: Peted | last post by:
Hi, im moving a project from vs2005 to vs 2008. is doing so i have come across a compiler error regarding partial classes that i dont understand if anyone can explain it to me please the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.