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

P

pookiebearbottom

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?=

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; }
 
P

Pete Becker

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.
 
R

Ron Natalie

Artie said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top