Re: C++ 'extern inline' magic possible?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 28 February 2011 23:54, Kevin P. Fleming wrote:
> In a C++ application consisting of a large number of source files, I've got
> a situation where there is a non-member function that *some* users could
> inline (because they can see the definition of the classes involved in the
> function's parameters and return value) and other users cannot inline
> (because they are only know of those classes due to forward declarations,
> and the function accepts and returns pointers to objects).
>
> As it stands right now, the header file declares this function for all
> callers, and there is one source file that provides the definition; this
> works, because all callers call the function after the library that provides
> the function is dynamically linked into their process.
>
> The function, though, is terribly simple, and the function call overhead
> easily dwarfs the code the function actually runs (in one case, the function
> is nothing more than a wrapper for the equivalent of a static_cast<>). If
> the compiler could see the function body it would be able to inline it in a
> large number of cases, reducing code size and improving performance.
>
> I have three source files involved here:
>
> aF.h contains only a forward declaration of class Foo, and a declaration of
> 'void bar(const Foo *)'.
>
> a.h contains the declaration and definition of class Foo, and a declaration
> of 'void bar(const Foo *)'.
>
> a.cpp contains the definition of 'void bar(const Foo *)'.
>
> I would like to come up with some construction like the 'extern inline' that
> GCC supports for C mode, so that a.h could contain the declaration *and*
> definition of 'bar', allowing code that includes a.h to have 'bar' be
> inlined if the compiler chooses to do so (and leave an external reference to
> 'bar' if necessary so that the version built from a.cpp will be used). So
> far my attempts have only resulted in various re-definition or
> re-declaration errors.
>
> Is there a way to do this in C++ mode? I see lots of pages with useful
> details and example for C mode, but not for C++ mode. I am *not* specifying
> '-std' to the compiler at all, and am using GCC 4.4; I've tried using
> '-std=c99' but that doesn't seem to do what I want (with the definition in
> the header marked 'inline' and the definition in the source file marked
> 'extern inline').

-std=c99 says which dialect to use when compiling C, it doesn't do
anything when compiling C++

The C++ standard says an extern function declared inline must be
declared inline in all translation units where it appears, and that a
definition of an inline function must appear in every translation unit
that uses it.  That is quite different to the C99 concept of an
"inline definition" which can exist in addition to an alternative
extern definition.

I would define two functions with different names, one inline and one
not. The non-inline function could include the definition of the
inline function and call it, so there is no more overhead.  That's
probably not what you want though, as you need to call a different
name depending on whether the inline function is available or not.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux