Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   still confused about headers, inline functions, linking, and redefining (http://www.velocityreviews.com/forums/t455465-still-confused-about-headers-inline-functions-linking-and-redefining.html)

pookiebearbottom@yahoo.com 07-18-2006 03:58 PM

still confused about headers, inline functions, linking, and redefining
 
Let's say I have headers Sal.h with this class

class Sal
{
public:
int doit() { return 1;}
};

now I know that the compilier can choose NOT to inline this function.
So if I include this in two different libraries, they both choose to
NOT inline it, and then I link to both libs, am I going to get/should I
get a redefinition error?

-sal


=?iso-8859-1?q?Pedro_Lamar=E3o?= 07-18-2006 04:15 PM

Re: still confused about headers, inline functions, linking, and redefining
 

pookiebearbottom@yahoo.com wrote:
> Let's say I have headers Sal.h with this class
>
> class Sal
> {
> public:
> int doit() { return 1;}
> };
>
> now I know that the compilier can choose NOT to inline this function.
> So if I include this in two different libraries, they both choose to
> NOT inline it, and then I link to both libs, am I going to get/should I
> get a redefinition error?


You will get a redefinition error.

Say both library A and library B have a source file including this
header. This means that, say, a.cpp and b.cpp, after preprocessing,
will include the code above -- the declaration for class Sal and the
definition for Sal::doit.

If you link these libraries together, the linker will simply see two
definitions for the same name, and issue an error.

If you really want to, you can use "extern inline" to provide a
definition only for inlining -- and then provide another definition
somewhere, probably in a source file for the library, when the compiler
decides not to inline the call.

Something like this:

Sal.h:

class Sal
{
public:

extern inline
int doit() { return 1; }
};

Sal.cpp:

int Sal::doit() { return 1; }


Pete Becker 07-18-2006 05:09 PM

Re: still confused about headers, inline functions, linking, andredefining
 
pookiebearbottom@yahoo.com wrote:
> Let's say I have headers Sal.h with this class
>
> class Sal
> {
> public:
> int doit() { return 1;}
> };
>
> now I know that the compilier can choose NOT to inline this function.
> So if I include this in two different libraries, they both choose to
> NOT inline it, and then I link to both libs, am I going to get/should I
> get a redefinition error?
>


No. The compiler will handle it.

Pete Becker 07-18-2006 05:10 PM

Re: still confused about headers, inline functions, linking, andredefining
 
Pedro Lamarão wrote:

> pookiebearbottom@yahoo.com wrote:
>
>>Let's say I have headers Sal.h with this class
>>
>>class Sal
>>{
>> public:
>> int doit() { return 1;}
>>};
>>
>>now I know that the compilier can choose NOT to inline this function.
>>So if I include this in two different libraries, they both choose to
>>NOT inline it, and then I link to both libs, am I going to get/should I
>>get a redefinition error?

>
>
> You will get a redefinition error.
>


No, the compiler will handle it.

pookiebearbottom@yahoo.com 07-18-2006 05:18 PM

Re: still confused about headers, inline functions, linking, and redefining
 
Pete Becker wrote:
> Pedro Lamarão wrote:
>
> > pookiebearbottom@yahoo.com wrote:
> >
> >>Let's say I have headers Sal.h with this class
> >>

> > You will get a redefinition error.
> >

>
> No, the compiler will handle it.


This is why I get confused. Wouldn't it be a linker issue anyway? Is
this covered by any "standard"?


Artie Gold 07-18-2006 05:31 PM

Re: still confused about headers, inline functions, linking, andredefining
 
pookiebearbottom@yahoo.com wrote:
> Pete Becker wrote:
>
>>Pedro Lamarão wrote:
>>
>>
>>>pookiebearbottom@yahoo.com wrote:
>>>
>>>
>>>>Let's say I have headers Sal.h with this class
>>>>
>>>
>>>You will get a redefinition error.
>>>

>>
>>No, the compiler will handle it.

>
>
> This is why I get confused. Wouldn't it be a linker issue anyway? Is
> this covered by any "standard"?
>

It is covered by the C++ standard.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]

Ron Natalie 07-18-2006 05:55 PM

Re: still confused about headers, inline functions, linking, andredefining
 
Artie Gold wrote:

>>
>> This is why I get confused. Wouldn't it be a linker issue anyway? Is
>> this covered by any "standard"?
>>

> It is covered by the C++ standard.
>

Yes, the standard says that duplicates of inline functions are allowed
as long as they are the same sequence of tokens in the function definition.

This is actually the only practical difference inline does. Whether
the declaration does anything else is an implementation detail (a
implementation is free to not inline inline functions or to inline
those not declared inline).


All times are GMT. The time now is 10:36 AM.

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