473,957 Members | 5,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1735
On 2008-09-29 06:01:07 -0400, eb********@gmai l.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...@versatile coding.comwrote :
On 2008-09-29 06:01:07 -0400, ebony.s...@gmai l.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 objektorientier ter Datenverarbeitu ng
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...@versatile coding.comwrote :
On 2008-09-29 06:01:07 -0400, ebony.s...@gmai l.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...@gm ail.comwrote:
On Sep 29, 1:17 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-09-29 06:01:07 -0400, ebony.s...@gmai l.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 objektorientier ter Datenverarbeitu ng
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...@gmai l.com wrote:
On Sep 29, 2:17*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-09-29 06:01:07 -0400, ebony.s...@gmai l.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...@gmai l.com wrote:
On Sep 29, 4:44 pm, James Kanze <james.ka...@gm ail.comwrote:
On Sep 29, 1:17 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-09-29 06:01:07 -0400, ebony.s...@gmai l.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 objektorientier ter Datenverarbeitu ng
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
3720
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
1
1564
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 code I posted here didn't have namespaces in it. I had originally tried to compile the same code with namespaces. The one subtlety I missed was that overloaded operator definitions are namespace local. If I declare this in my header file: ...
7
2305
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 that page like http://machinename/dir1/hellp.aspx instead of running that page it starts downloding ...whats missing here ....why the aspx engine not running the page....
4
118832
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 character into a number?????? In Oracle, it is:
11
2214
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; } #endif that definition of foo() will only appear once in any given translation
14
3007
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
3616
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
2160
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 System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
17
5840
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
10046
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11278
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
11447
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
10757
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...
1
8337
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
6282
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
6411
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5023
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4613
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.