This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: g++: a bug or a feature ???


Gokhan Kisacikoglu <kisa@centropolisfx.com> writes:

> This is the correct code:
> 
> 
> -----------------------
> //main.cc
> #include <iostream>
> #include "test.hh"
> 
> inline void g() { cerr<<"Main.g()\n" ; }
> 
> void main() {
>     g();    // wish to activate g() in main.cc
> 
>     f();     // wish to activate g() in test.cc through f().
> }
> //--------end of main.cc
> 
> //test.cc
> #include <iostream>
> #include "test.hh"
> 
> namespace test_cc
> {
>     inline void g() { cerr<<"Test.g()\n" ; }
> }
> 
> void f() {
>     test_cc :: g();    // wish to activate g() in test.cc
> }
> 
> 	---- OR ---------
> 
> using namespace test_cc;	// in the scope of test.cc
> 
> void f() {
>     g();    // wish to activate g() in test.cc
> }

Why simply don't use an anonymous namespace? Why main.cc's 'g'
function is not wrapped in a namespace as well?

As a rule of thumb, put all your file-scope identifiers inside
anonymous namespaces. You will save some pains.

I guess that the problem described by the OP will not happen if it
declared the functions 'static' (which has a similar beahvior to
anonymous namespaces WRT this issue plus some dramatical
differences). And I guess too that the problem does not shows building
in release mode (that is, when 'inline' is honored).

 
> //--------end of test.cc
> 
> // test.hh
> 
> void f();
> //--------end of test.hh
> 
> 
> The compiler has to make a decision on which g() to link with, obviously
> the first one that is available is the one in main.

Here, the real problem is that you are mixing 'inline' (which
overrides the One Definition Rule) with multiple global definitions
*and* debug mode.

Other implementations would not allow this. It's 'ld' behavior what
allows it. (Not necessarily saying that 'ld' is broken...)

-- 
Oscar


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]