Inline member functions

K

kelvSYC

I'm not sure if it violates standards or if it's just one of gcc's
quirks, but on gcc with Mac OS X (at least with my installation), for
some reason inline member functions that are not part of the class
declaration always produce linker errors.

For example, take a piece of my code:

// random access iterator for Cocoa Foundation's NSArray
// technically obj-c++, but only the c++ part that is relevant
class NSArrayIterator : std::iterator<...> {
// template arguments above not really relevant
unsigned int index;

public:
NSArrayIterator& operator++() { index++; return *this; }
};

The above compiles and links fine, but the one below gives out a linker
error

(NSArrayIterator.h)
class NSArrayIterator : std::iterator<...> {
unsigned int index;

//...
public:
NSArrayIterator& operator++();
};

(NSArrayIterator.mm)
inline NSArrayIterator& NSArrayIterator::eek:perator++() {
index++;
return *this;
}

Is this supposed to be standard behavior (that it compiles but doesn't
link), a gcc quirk, or some other quirk? Is this issue similar to that
export and templates? If it's a gcc or linker thing, what should I set
in order for the second one to properly link?
 
J

Jack Klein

I'm not sure if it violates standards or if it's just one of gcc's
quirks, but on gcc with Mac OS X (at least with my installation), for
some reason inline member functions that are not part of the class
declaration always produce linker errors.

For example, take a piece of my code:

// random access iterator for Cocoa Foundation's NSArray
// technically obj-c++, but only the c++ part that is relevant
class NSArrayIterator : std::iterator<...> {
// template arguments above not really relevant
unsigned int index;

public:
NSArrayIterator& operator++() { index++; return *this; }
};

The above compiles and links fine, but the one below gives out a linker
error

(NSArrayIterator.h)
class NSArrayIterator : std::iterator<...> {
unsigned int index;

//...
public:
NSArrayIterator& operator++();
};

(NSArrayIterator.mm)
inline NSArrayIterator& NSArrayIterator::eek:perator++() {
index++;
return *this;
}

Is this supposed to be standard behavior (that it compiles but doesn't
link), a gcc quirk, or some other quirk? Is this issue similar to that
export and templates? If it's a gcc or linker thing, what should I set
in order for the second one to properly link?

This is not similar to export for templates. The inline keyword
requests that the compiler substitute the executable code for the
function body in place of the call. It can do this if and only if it
has the source of the function in scope, to generate the code.

If you want the compiler to have the option of honoring your request
to inline the member functions, they need to be in the header.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top