C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

when an inline function doesn't inline, why doesn't compile

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Manas
Guest





PostPosted: Thu Aug 28, 2003 3:05 pm    Post subject: when an inline function doesn't inline, why doesn't compile Reply with quote



hi,
suppose i have the following files:
one.cpp
----------------------------------------------
#include <iostream>

using namespace std::cout

inline void g(void)
{
cout<<"One cppn";
}

void f(void);

int main(void)
{
f();
g();
return 0;
}
----------------------------------------------------

two.cpp
----------------------------------------------------
#include
using namespace std::cout

inline void g(void)
{
cout<<"two cppn";
}

void f(void)
{
cout<<"F of two cppn";
g();
}
---------------------------------------------------------

Now when i compile and run the code, i get the following output:
F of two cpp
One cpp
One cpp

Clearly the function is not inlined(actually gcc doesn't inline if the
code is not optimized and by default the code is not optimized).
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.
Also, why does it take definition of g() in one.cpp , not that in
two.cpp.

thanks
manas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Aug 29, 2003 8:34 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote



In article <9635763f.0308272107.abddb99 (AT) posting (DOT) google.com>, Manas
<bhatt_manas (AT) hotmail (DOT) com> writes
Quote:
suppose i have the following files:
one.cpp
----------------------------------------------
#include
using namespace std::cout

That will not compile.

Quote:

inline void g(void)
{
cout<<"One cppn";
}

void f(void);

int main(void)
{
f();
g();
return 0;
}
----------------------------------------------------

two.cpp
----------------------------------------------------
#include
using namespace std::cout

inline void g(void)
{
cout<<"two cppn";
}

void f(void)
{
cout<<"F of two cppn";
g();
}
---------------------------------------------------------

Now when i compile and run the code, i get the following output:
F of two cpp
One cpp
One cpp

No you do not. The code does not compile.

Quote:

Clearly the function is not inlined(actually gcc doesn't inline if the
code is not optimized and by default the code is not optimized).
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.

Because the language explicitly requires that it does not do so.

Quote:
Also, why does it take definition of g() in one.cpp , not that in
two.cpp.

I think you need to check on the meaning of the inline keyword. It
explicitly allows multiple definitions and it is up to the
implementation to deal with the result.

If the definition of an inline function is not the same in two TUs then
you have broken the one definition rule and are in the land of undefined
behaviour.


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joshua Lehrer
Guest





PostPosted: Fri Aug 29, 2003 8:44 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote



[email]bhatt_manas (AT) hotmail (DOT) com[/email] (Manas) wrote in message news:<9635763f.0308272107.abddb99 (AT) posting (DOT) google.com>...
Quote:
hi,
suppose i have the following files:
one.cpp
----------------------------------------------
#include
using namespace std::cout

inline void g(void)
{
cout<<"One cppn";
}

void f(void);

int main(void)
{
f();
g();
return 0;
}
----------------------------------------------------

two.cpp
----------------------------------------------------
#include
using namespace std::cout

inline void g(void)
{
cout<<"two cppn";
}

void f(void)
{
cout<<"F of two cppn";
g();
}
---------------------------------------------------------

Now when i compile and run the code, i get the following output:
F of two cpp
One cpp
One cpp


I'm going to take a stab at this one, and warn you, that I'm a bit
sketchy in this area.

I *think* that you have violated the one-definition rule. The fact
that the code is inlined has nothing to do with it's linkage. You
have two symbols named "g" with the same signature. You have asked
the compiler to optimize the code by inlining it, but it still must
make extern'ed symbols. I would say that you *should* get an error
from your linker.

However, on my system, I get no error, and I get the expected:

F of two cpp
two cpp
One cpp

Either way, the "correct" way is to hide "g" from the other
translation units. The old way was to specify static linkage:

static inline void g() { }

The new way is to wrap it in an anonymous namespace:

namespace {
inline void g() { }
}

joshua lehrer
factset research systems
NYSE:FDS

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
White Wolf
Guest





PostPosted: Fri Aug 29, 2003 11:21 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Manas wrote:
[SNIP]
Quote:
Clearly the function is not inlined(actually gcc doesn't inline if the
code is not optimized and by default the code is not optimized).
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.
Also, why does it take definition of g() in one.cpp , not that in
two.cpp.

You have defined two non-static functions with different body and exactly
the same name. It does not matter a bit if they are inline or not. As long
as they are non-static (and not hidden in an unnamed namespace) you have
just violated the ODR (One Definition Rule) and it is undefined behaviour.
Your compiler can do whatever it wants to do and it does not even need to
explain it (in a documentation etc.).

--
WW aka Attila



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Aug 29, 2003 11:28 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Manas wrote:
Quote:
one.cpp
inline void g(void)
{
cout<<"One cppn";
}

two.cpp
inline void g(void)
{
cout<<"two cppn";
}

Violation of the 'one definition rule'. Undefined behaviour, anything can
happen.

Anyways, some compilers do warn and inline is not mandatory to the
compiler.

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Aug 30, 2003 10:12 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote


[...]
Quote:
No you do not. The code does not compile.

The code should not compile, according to the standard. I've got lots
of code that should not compile, according to the standard, but does.
(Not intentionally, of course, but I was writing code before there was a
standard.) In this case, it's not a case of accepting prestandard code,
but you still can't exclude a buggy compiler.

Quote:
Clearly the function is not inlined(actually gcc doesn't inline if
the code is not optimized and by default the code is not optimized).
Question is :

If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.

Because the language explicitly requires that it does not do so.

Almost. The standard doesn't say anything about warnings, so a warning
would be permitted. A compile time warning would be very, very
difficult, since it would require the compiler to know files what I will
later link. A link time warning would be quite possible, however.

In his particular case, of course, even a link time error is
acceptable. Since he violated the one definition rule, the system can
do whatever it wants. IMHO, a link time error or warning would be a
good thing in such cases. And technically, it shouldn't be too
difficult to do, except in portable compilers which use the native
linker (e.g. EDG, g++): just stuff an MD5 hash of whatever comes under
the one definition rule in the object file, and check that all instances
have the same hash. Why no one has bothered to do it to date is beyond
me.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrey Tarasevich
Guest





PostPosted: Sat Aug 30, 2003 1:21 pm    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Manas wrote:
Quote:
...
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.
...

Why should it? And why do you care?

The C++ standard explicitly states that inline functions are allowed to
have multiple definitions in multiple translation units (at most one per
translation unit, of course). Just read the ODR- and inline-related
parts of the standard.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Mirwaisi
Guest





PostPosted: Sat Aug 30, 2003 11:43 pm    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Stroustrup:TC++PL 9.2.3

That is, two definitions of a class, template,
or inline function are accepted as examples of the
same unique definition if and only if
[1] they appear in different translation units, and
[2] they are tokenfortoken identical, and
[3] the meanings of those tokens are the same in
both translation units.

and

3.2 para 3 iso 14882
... An inline function shall be defined in every
translation unit in which it is used.

3.2 para 5 iso 14882
There can be more than one definition of a class type
(clause 9), enumeration type (7.2), inline function
with external linkage (7.1.2), class template (clause
14), nonstatic function template (14.5.5), static data
member of a class template (14.5.1.3), member function template
(14.5.1.1), or template
specialization for which some template parameters are
not specified (14.7, 14.5.4) in a program provided
that each definition appears in a different
translation unit, and provided the definitions
satisfy the following requirements. ...


Therefore:

<foo.cpp>
inline void f() {}
<bar.cpp>
inline void f() {}

is legal and defined by the ODR

-Jeff Mirwaisi

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Andrey Tarasevich
Guest





PostPosted: Sun Aug 31, 2003 8:01 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Andrey Tarasevich wrote:
Quote:
...
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.
...

Why should it? And why do you care?

The C++ standard explicitly states that inline functions are allowed to
have multiple definitions in multiple translation units (at most one per
translation unit, of course). Just read the ODR- and inline-related
parts of the standard.
...

Although the problem with your code is not related to actual inlining or
non-inlining. You made two non-matching definitions of the same inline
function with external linkage in two different translation units. That
violates ODR for inline functions.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Aug 31, 2003 12:22 pm    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

In article <Xns93E747412ABCFjeffmirwaisiyahoocom (AT) 207 (DOT) 217.77.202>, Jeff
Mirwaisi <jeff_mirwaisi (AT) yahoo (DOT) com> writes
Quote:
Therefore:

foo.cpp
inline void f() {}
bar.cpp
inline void f() {}

is legal and defined by the ODR

But

inline void f(){NULL}

would break the ODR. We had immense difficulty with defining this aspect
of the ODR because we could find no set of rules that allowed all
clearly functionally equivalent definitions or even all definitions that
generated the same code that did not also let in cases which were
clearly breaches of the ODR. The most uncomfortable aspect was the
recognition that token-for-token equivalence was not sufficient because
the local file context could modify the meaning of a token.

One implementation (Metaware High C++) did diagnose some ODR breaches at
link time. If two definitions of the 'same' function were not byte for
byte identical in the object files being linked it issued a diagnostic.


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daveed Vandevoorde
Guest





PostPosted: Sun Aug 31, 2003 5:18 pm    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote:
[...]
Quote:
Question is :
If the compile can't inline a function, why doesn't it give a compile
time warning for multiple function definition.

Because the language explicitly requires that it does not do so.

I don't think it does.

First, even if the program were valid, an implementation is allowed
to issue diagnostics as long as it accepts the program. (E.g., the
EDG front end has an option to issue such a diagnostic if its simple
inliner is enabled but unable to inline an inline function.)

Second (and ignoring the syntactical error you correctly identified),
this code violates the one definition rule by providing two different
definitions of an inline function. That results in undefined behavior
and therefore a compiler is allowed to issue warning, hard errors,
or anything else for this. (And again, the EDG front end can be
configured to issue such errors.)

Quote:
Also, why does it take definition of g() in one.cpp , not that in
two.cpp.

Many compilers issue the non-inlined copy in a special object
file section identified by name (e.g., the mangled name of the
function). When the linker sees several such sections with the
same name, it discards all but one of them. The linker does not
check if they're identical because small differences in the
code-generation state can result in harmless differences from
one translation unit to another.

Try changing the order of the files on the command line:
I suspect that might actually change the output.

Daveed

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Mon Sep 01, 2003 1:11 am    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote



Francis Glassborow schrieb:

Quote:

I think you need to check on the meaning of the inline keyword. It
explicitly allows multiple definitions and it is up to the
implementation to deal with the result.

If the definition of an inline function is not the same in two TUs then
you have broken the one definition rule and are in the land of undefined
behaviour.

Say I put the definition of a simple function into a header file:

in foo.h:

void foo()
{std::cout << "foo() called";}


The header will be #included by various files, so foo appears in different
translation units.
Is it now compulsory to declare foo() as inline (by prefixing it with the
'inline' - keyword), or is the definition as given above (without 'inline')
OK?


regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Sep 01, 2003 7:05 pm    Post subject: Re: when an inline function doesn't inline, why doesn't com Reply with quote

In article <3F51CC1A.E6634C0C (AT) unet (DOT) univie.ac.at>, Thomas Mang
<a9804814 (AT) unet (DOT) univie.ac.at> writes
Quote:

Say I put the definition of a simple function into a header file:

in foo.h:

void foo()
{std::cout << "foo() called";}


The header will be #included by various files, so foo appears in different
translation units.
Is it now compulsory to declare foo() as inline (by prefixing it with the
'inline' - keyword), or is the definition as given above (without 'inline')
OK?
If you put it in a header you must either make it inline or use some

mechanism that allows multiple uses of a name (such as putting it in an
unnamed namespace or qualifying it as static).

The basic point is that you put things in header files that need to be
available across TUs. In general those are declarations of names +
definitions of types.

If you want to make inlining a function easy for the implementation
(modern implementations are becoming increasingly adept at inlining
small functions at link time) it has to go in a header. There is no
other motive for putting the definition of an ordinary function in a
header file other than not understanding the C and C++ mechanism for
separation of declaration from definition.

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.