开发者

Static inline methods?

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::开发者_如何学Cbar(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.


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.


Generally inline-able functions are implemented where they are declared (in the header file). The compiler is free to inline functions as you have them, but you can't force it to inline anything. If you're using Visual C++, enable "inline any suitable", "link-time code generation" and "favor fast code".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜