开发者

Are methods in classes using the 'Curiously Recurring Template Pattern' inlined by a modern c++ compiler

I have a performance critical piece of code for which I am considering using the CRTP. My question is to what extent most compilers are able to optimize the code. In particular I am wonde开发者_运维问答ring if the compiler can inline (when appropriate) methods. For example, in the following code:

template <class Derived> 
struct Base
{
    void interface()
    {
        // ...
        static_cast<Derived*>(this)->implementation();
        // ...
    }
};

struct Derived : Base<Derived>
{
    void implementation();
};

would a call to object.interface() yield the same performance as a call to object.implementation()


Overwhelmingly probably. Of course, there's only one way to be sure.


With optimization turned on, and if the compiler considers this to be worth inlining, yes.

What's good in CRTP compared to dynamic dispatch, is that from compiler's point of view it's a regular function call.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜