473,386 Members | 1,726 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,386 software developers and data experts.

ODR: A simple question

Dear all
Hi

I encountered a simple but IMO important problem about the C++ linkage
model and One Definition Rule. Why the following code link?

file1.cpp
int x;

file2.cpp
double x;

x was defined in two different translation units with different types.
It breaks the ODR.
The following code is also linked:

file1.cpp
int x;

file2.cpp
extern double x;
It is obvious the type x in declaration and definition are different.
I ran and tested codes in Visual Studio 6.0 and .Net 2005.

Thank you in advance
- Saeed Amrollahi
Sep 29 '08 #1
6 1713
On 2008-09-29 06:01:07 -0400, eb********@gmail.com said:
>
I encountered a simple but IMO important problem about the C++ linkage
model and One Definition Rule. Why the following code link?

file1.cpp
int x;

file2.cpp
double x;

x was defined in two different translation units with different types.
It breaks the ODR.
The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.

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

Sep 29 '08 #2
On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
I encountered a simple but IMO important problem about the
C++ linkage model and One Definition Rule. Why the following
code link?
file1.cpp
int x;
file2.cpp
double x;
x was defined in two different translation units with
different types. It breaks the ODR.
The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.
And the relatively low quality of most linkers. It wouldn't be
very hard to implement, if the linker supported it. For
historical reasons, most linkers don't.

--
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
Sep 29 '08 #3
On Sep 29, 2:17*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
I encountered a simple but IMO important problem about the C++ linkage
model and One Definition Rule. Why the following code link?
file1.cpp
int x;
file2.cpp
double x;
x was defined in two different translation units with different types.
It breaks the ODR.

The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Sorry for a day delay. I was out of office.
Thank you pete for you answer, but I didn't completely convince.
I thought may be some standard conversions were made (double to int or
vice versa)
I changed double to std::string, but program run. Just when I declare
the variable
with extern, I have to specifiy the type and compiler find the real
type.
Which clause or section of C++ standard draft mention your answer?

Regards,
- Saeed Amrollahi
Sep 30 '08 #4
On Sep 29, 4:44*pm, James Kanze <james.ka...@gmail.comwrote:
On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
I encountered a simple but IMO important problem about the
C++ linkage model and One Definition Rule. Why the following
code link?
file1.cpp
int x;
file2.cpp
double x;
x was defined in two different translation units with
different types. *It breaks the ODR.
The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.

And the relatively low quality of most linkers. *It wouldn't be
very hard to implement, if the linker supported it. *For
historical reasons, most linkers don't.

--
James Kanze (GABI Software) * * * * * * email:james.ka...@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
Sorry for a day delay. I was out of office.
Thank you for your answer. AFAIR, Stroustrup mentioned the poor
quality of linkers in 1st edition
of his book. Where can I find, C++ Linker specific information?

Regards,
Saeed Amrollahi
Sep 30 '08 #5
On 30 Sep, 19:58, ebony.s...@gmail.com wrote:
On Sep 29, 2:17*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
I encountered a simple but IMO important problem about the C++ linkage
model and One Definition Rule. Why the following code link?
file1.cpp
int x;
file2.cpp
double x;
x was defined in two different translation units with different types..
It breaks the ODR.
The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.

Sorry for a day delay. I was out of office.
Thank you pete for you answer, but I didn't completely convince.
I thought may be some standard conversions were made (double to int or
vice versa)
I changed double to std::string, but program run. Just when I declare
the variable
with extern, I have to specifiy the type and compiler find the real
type.
Which clause or section of C++ standard draft mention your answer?
See 3.2 One definition rule [basic.def.odr]

Regards,
* - Saeed Amrollahi
Oct 1 '08 #6
On Sep 30, 8:06 pm, ebony.s...@gmail.com wrote:
On Sep 29, 4:44 pm, James Kanze <james.ka...@gmail.comwrote:
On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
I encountered a simple but IMO important problem about the
C++ linkage model and One Definition Rule. Why the following
code link?
file1.cpp
int x;
file2.cpp
double x;
x was defined in two different translation units with
different types. It breaks the ODR.
The language definition doesn't require compilers to
diagnose violations of the ODR. Code like this has
undefined behavior. As a practical matter, recognizing
errors like this is expensive, given C++'s separate
compilation model.
And the relatively low quality of most linkers. It wouldn't
be very hard to implement, if the linker supported it. For
historical reasons, most linkers don't.
Thank you for your answer. AFAIR, Stroustrup mentioned the poor
quality of linkers in 1st edition
of his book.
Yes. C++ was originally designed to use existing linkers, with
a minimum of additional baggage. (There was, IIRC, always a
"pre-linker", which generated the code for dynamic
initialization. But that was it.)

Today, of course, templates require a lot more support, and much
of it could be used to allow greater freedom elsewhere as well.
Where can I find, C++ Linker specific information?
To tell the truth, I don't know. Linkers seem to be the poor
relative in the language translation area: you'll find all sorts
of information concerning compiler theory and the like, but very
little about linkers.

I think the question has been raised once or twice in
comp.compilers; you might try there. (Not because the question
is off subject here, but because that's where the people who
actually work on these things hang out.)

--
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
Oct 1 '08 #7

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
1
by: Steven T. Hatton | last post by:
A while back I posted some code along with an error message I got when I tried to compile it. I thought at one point I had solved all the problems. But then I tried it again, and if failed. The...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
11
by: Steven T. Hatton | last post by:
If I have a non-inline function definition in a header file with header guards like this: #ifndef HEADER_GUARD_H #define HEADER_GUARD_H int foo(){ return 42; } namespace { bar() { return 666;...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.