Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Multiple functions (one version being inline and other beingnon-inline) (http://www.velocityreviews.com/forums/t594535-multiple-functions-one-version-being-inline-and-other-beingnon-inline.html)

Rahul 02-27-2008 10:59 AM

Multiple functions (one version being inline and other beingnon-inline)
 
Hi Everyone,

I have the following code,

file1.cpp
---------

#include <cstdio>

inline int sample1()
{
printf("1::sample1\n");
return(0);
}

int main()
{
sample1();
return(0);
}


file2.cpp
---------

#include <cstdio>

int sample1()
{
printf("2::sample1\n");
return(0);
}


when i build both the files and exeucte, i get the following output,

2::sample1


I expected a linker error, as file2.o is exporting sample1 which is
already available in file1.o... What does the standard indicate for
such scenarios?

Thanks in advance !!!

Marcel Müller 02-27-2008 06:28 PM

Re: Multiple functions (one version being inline and other beingnon-inline)
 
Rahul schrieb:
> I expected a linker error, as file2.o is exporting sample1 which is
> already available in file1.o... What does the standard indicate for
> such scenarios?


You must not define the same object twice in a different way. Otherwise
- as you might guess - undefined behaviour. (Look for the ODR.)


Marcel

nurxb01 02-28-2008 06:34 AM

Re: Multiple functions (one version being inline and other beingnon-inline)
 
On Feb 27, 11:28*pm, Marcel Müller <news.5.ma...@spamgourmet.com>
wrote:
> Rahul schrieb:
>
> > I expected a linker error, as file2.o is exporting sample1 which is
> > already available in file1.o... What does the standard indicate for
> > such scenarios?

>


I dont have knowledge of what standard says but I tried this with g++
2.95.3 and g++ is doing is
1] Keeps the sample1 funcion in file1.cpp as weak symbole in the
object file ( Probably because you have made it inline )
2] sample1 funcion in the file2.cpp is treated as Global symbol.

So when you link this two object modules the Global symbole gets
priority over Weak Symbol and you dont get any linker errors.

If you remove inline in sample1 funcion form file1.cpp, you should get
linker error.


James Kanze 02-28-2008 03:28 PM

Re: Multiple functions (one version being inline and other beingnon-inline)
 
On Feb 27, 11:59 am, Rahul <sam_...@yahoo.co.in> wrote:
> I have the following code,


> file1.cpp
> ---------


> #include <cstdio>
>
> inline int sample1()
> {
> printf("1::sample1\n");
> return(0);
> }


> int main()
> {
> sample1();
> return(0);
> }


> file2.cpp
> ---------


> #include <cstdio>


> int sample1()
> {
> printf("2::sample1\n");
> return(0);
> }


> when i build both the files and exeucte, i get the following output,


> 2::sample1


> I expected a linker error, as file2.o is exporting sample1
> which is already available in file1.o... What does the
> standard indicate for such scenarios?


It's undefined behavior, so anything the compiler does with it
is correct. §7.1.2/4: "If a function with external linkage is
declared inline in one translation unit, it shall be declared
inline in all translation units in which it appears; no
diagnostic is required."

--
James Kanze (GABI Software) email:james.kanze@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


All times are GMT. The time now is 09:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.