Okay,
Here is what I'm trying to do... Right now it is compiling but failing at linking... LNK2001
I want the methods static because there are no member variables, however I also want them inline for the speedups they provide.
What is the best way to do this? Here is what I have in a nutshell:
/* foo.h */
class foo
{
static void bar(float* in);
};
/* foo.cpp */
inline void foo::bar(float* in)
{
// some dark magic here
}
I'm trying to do this because I want to be able to go:
foo::bar(myFloatPtr);
foo doesn't have any member variables... it doesn't make sense to.
Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.
Static local variables are not allowed to be defined within the body of an inline function.
Static means the function should not be visible outside of the translation unit, inline is a hint to the compiler the programmer would like to have this function inlined. Those two are not related. Using static inline makes sense when the inlined function is not used outside of the translation unit.
A static inline function is, in practice, likely (but not certain) to be inlined by some good optimizing compiler (e.g. by GCC when it is given -O2 ) at most of its call sites. It is defined in a header file, because it then could be inlined at most call sites (perhaps all of them).
If you are calling bar from another cpp file, other than foo.cpp, it needs to be in a header file.
First, I would put them in a namespace
instead, because there is no logic at all in that "class"
. Second, you may define the functions body in the header file directly to allow the compiler to see them. Otherwise, you need whole program optimization
to be done by the linker for inlining those functions(AFAIK).
You must define your inline function in the header file, not a separate implementation file. The definitions are needed when the header file is #included, if they are hoped to be inlined, after all.
The link failure you are seeing is because the declaration (in the header file) does not inform the compiler that the method is to be inline, whereas the implementation is inline, so not available for linking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With