473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

name decoration - why does this example code compile?

Hi,

I'm about half way through Bruce Eckels thinking in C++ Vol 1. He gives
a very short example of how compiler function name decoration helps
remove subtle bugs by type-safe linkage. However, upon looking at the
example i understand why it fails to link but don't understand why it
even compiles. Here's the code, which split between 2 files Def.cpp and
Use.cpp:

//File Def.cpp
//Function definition
void f(int) {}

In the second file, the function is misdeclared and then called:
//File Use.cpp
//Function misdeclaration
void f(char);

int main() {
f(1); // Causes a linker error
}

I thought that C++ forces the user to declare all functions before use
so it can perform compile time error checking e.g. against proper type
usage. In the above example i would have expected the compiler to throw
up an error because in creating the object file for Use.cpp it would
see that i declare a function that takes a char but actually call it
with an integer. However, it does compile, and only barfs during the
link stage as it can't find the function definition of the version that
takes a char argument.
Can anyone tell me why it compiles?
Also, as a side question i also thought C++ forces the user to define
function arguments with a variable name, but again in Def.cpp the
function is defined only with a variable type.

Thanks in advance

May 17 '06 #1
8 2341
mastermagrath wrote:
Hi,

I'm about half way through Bruce Eckels thinking in C++ Vol 1. He gives
a very short example of how compiler function name decoration helps
remove subtle bugs by type-safe linkage. However, upon looking at the
example i understand why it fails to link but don't understand why it
even compiles. Here's the code, which split between 2 files Def.cpp and
Use.cpp:

//File Def.cpp
//Function definition
void f(int) {}

In the second file, the function is misdeclared and then called:

//File Use.cpp
//Function misdeclaration
void f(char);

int main() {
f(1); // Causes a linker error
}

I thought that C++ forces the user to declare all functions before use
so it can perform compile time error checking e.g. against proper type
usage.
Yes it does.
In the above example i would have expected the compiler to throw
up an error because in creating the object file for Use.cpp it would
see that i declare a function that takes a char but actually call it
with an integer.
An int can be implictly converted to a char. This would have failed:

class A {};

void f(A a);

int main()
{
f(3); // cannot convert int 3 to an A
}
However, it does compile, and only barfs during the
link stage as it can't find the function definition of the version that
takes a char argument.
Can anyone tell me why it compiles?
It compiles because the code is legal. It doesn't link because void
f(char) is never defined.

Understand that the compiler has no "memory" between translation units.
As a programmer, you may know that f(int) and f(char) are intended to
be the same function, but for the compiler, everything is perfectly
kosher. However, the linker, which is in charge of "mapping" function
declaration to function definition, cannot find the required definition
and thus reports an error.
Also, as a side question i also thought C++ forces the user to define
function arguments with a variable name, but again in Def.cpp the
function is defined only with a variable type.


You thought wrong.
Jonathan

May 17 '06 #2
The number 1 is also a char. A char is just a number between 0 and 255.
Characters like 'A' are just aliases for the ascii number ( i.e. 'A' =
0x41).
That's why the type-checking doesn't fail and only the linker complains.

May 17 '06 #3
l...@gmx.de wrote:

Please learn to quote correctly
(http://en.wikipedia.org/wiki/Top-pos...ing.2Ftrimming and
http://cfaj.freeshell.org/google/).
The number 1 is also a char.
No. The litteral 1 is an int, but an int can be implictly converted to
a char.
A char is just a number between 0 and 255.
No. A char is a data type which is at least 1 byte and a byte is at
least 8 bits. Also, whether it is signed or not is implementation
defined. To be safe, you can only assume a char has a range of [0,
127].
Characters like 'A' are just aliases for the ascii number ( i.e. 'A' =
0x41).
Again, no. Characters have the value of the underlying character set:
ASCII on ASCII machines, EBCDIC on EBCDIC machines, etc.
That's why the type-checking doesn't fail and only the linker complains.


The reason it doesn't fail is simply that chars are implictly
convertible to int.

Check your facts before posting.
Jonathan

May 17 '06 #4
dj
Jonathan Mcdougall wrote:
mastermagrath wrote:
Hi,

I'm about half way through Bruce Eckels thinking in C++ Vol 1. He gives
a very short example of how compiler function name decoration helps
remove subtle bugs by type-safe linkage. However, upon looking at the
example i understand why it fails to link but don't understand why it
even compiles. Here's the code, which split between 2 files Def.cpp and
Use.cpp:

//File Def.cpp
//Function definition
void f(int) {}

In the second file, the function is misdeclared and then called:

//File Use.cpp
//Function misdeclaration
void f(char);

int main() {
f(1); // Causes a linker error
}

I thought that C++ forces the user to declare all functions before use
so it can perform compile time error checking e.g. against proper type
usage.


Yes it does.
In the above example i would have expected the compiler to throw
up an error because in creating the object file for Use.cpp it would
see that i declare a function that takes a char but actually call it
with an integer.


An int can be implictly converted to a char. This would have failed:

class A {};

void f(A a);

int main()
{
f(3); // cannot convert int 3 to an A
}
However, it does compile, and only barfs during the
link stage as it can't find the function definition of the version that
takes a char argument.
Can anyone tell me why it compiles?


It compiles because the code is legal. It doesn't link because void
f(char) is never defined.

Understand that the compiler has no "memory" between translation units.
As a programmer, you may know that f(int) and f(char) are intended to
be the same function, but for the compiler, everything is perfectly
kosher. However, the linker, which is in charge of "mapping" function
declaration to function definition, cannot find the required definition
and thus reports an error.
Also, as a side question i also thought C++ forces the user to define
function arguments with a variable name, but again in Def.cpp the
function is defined only with a variable type.


You thought wrong.
Jonathan


What is the difference if the function f(char) in file use.cpp is
declared extern?
May 17 '06 #5
dj wrote:
Jonathan Mcdougall wrote:
mastermagrath wrote:
//File Def.cpp
//Function definition
void f(int) {}

In the second file, the function is misdeclared and then called:

//File Use.cpp
//Function misdeclaration
void f(char);

int main() {
f(1); // Causes a linker error
}

However, it does compile, and only barfs during the
link stage as it can't find the function definition of the version that
takes a char argument.
Can anyone tell me why it compiles?


It compiles because the code is legal. It doesn't link because void
f(char) is never defined.


What is the difference if the function f(char) in file use.cpp is
declared extern?


None whatsoever. A function always has external linkage (unless it is
static), so adding "extern" is redundant.
Jonathan

May 17 '06 #6
On 17 May 2006 05:19:57 -0700, "Jonathan Mcdougall"
<jo************ ***@gmail.com> wrote in comp.lang.c++:
l...@gmx.de wrote:

Please learn to quote correctly
(http://en.wikipedia.org/wiki/Top-pos...ing.2Ftrimming and
http://cfaj.freeshell.org/google/).
The number 1 is also a char.
No. The litteral 1 is an int, but an int can be implictly converted to
a char.
A char is just a number between 0 and 255.


No. A char is a data type which is at least 1 byte and a byte is at


No. A char is a data type which is exactly 1 byte, by definition. Even
on platforms where CHAR_BIT is 16 or 32 (fairly common today on DSPs).
least 8 bits. Also, whether it is signed or not is implementation
defined. To be safe, you can only assume a char has a range of [0,
127].


The rest is OK.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
May 18 '06 #7
Jack Klein wrote:
<jo************ ***@gmail.com> wrote in comp.lang.c++:
No. A char is a data type which is at least 1 byte and a byte is at


No. A char is a data type which is exactly 1 byte, by definition. Even
on platforms where CHAR_BIT is 16 or 32 (fairly common today on DSPs).


I was already thinking about the "at least 8 bits" part. My mistake.
Jonathan

May 18 '06 #8
Thanks folks, that clears it up for me

May 18 '06 #9

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

Similar topics

0
1174
by: Sean Ross | last post by:
Set, Mark, and Apply Decoration Provide a means to set the decoration that will be applied to all marked functions (and maybe classes) within a scope (module, class, or function). For example purposes, I will use a decor object that would presumably be made available in each scope (via locals, perhaps). I'll say that it has (atleast) a...
15
2111
by: middletree | last post by:
How do I request the actual page name that I'm on? This is going to go into an include file, and depending on which page I'm on, I'd like to do different things. I'm looking for "pagename.asp"
14
6328
by: Andrew Tang | last post by:
Hi, I need some help with understanding this piece of css. <u>This is a <a href="#" style="text-decoration:none;">hyperlink</a> with surrounding underlining</u> From what I understand of css, the styling in <a> should override its parent. But it doesnt seem to work in this case, the whole sentence is still underlined even though I have...
3
4498
by: Christian Roth | last post by:
What I want: <p style="text-decoration: line-through"> Stricken <span style="text-decoration: none">not stricken</span> </p> , where "Stricken " should be the only thing that is lined through. According to CSS2.1, this does not work (and all browsers I tested draw
18
2244
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object): def __init__(self, *args, **keyword_args): self.__dict__.update(
8
2558
by: Bern | last post by:
hi all, what is the rule for decorating a symbol in c++?
6
6512
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying "yes", but i vaguely remember having read that it's not exactly like that one might say that "name mangling" is a part of c++ compiler specification...
5
2035
by: David Alasiga | last post by:
Does g++ have option to disable function name commingle. When I compile like: g++ -S Test.cpp the file Test.s has function name commingled. For example, function name foo becomes like __foo79emskd8. Is there any options which output function name as original instead of commingled name? Thanks in advance.
6
4252
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt support this, but I dont know if gcc can , thanks in advance.
0
7499
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...
0
7432
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...
0
7689
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. ...
0
6022
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...
1
5359
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...
0
3490
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...
1
1919
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
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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...

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.