我有一段性能关键的代码,我正在考虑使用 CRTP。我的问题是大多数编译器能够在多大程度上优化代码。特别是我想知道编译器是否可以内联(在适当的时候)方法。例如,在以下代码中:
template <class Derived>
struct Base
{
void interface()
{
// ...
static_cast<Derived*>(this)->implementation();
// ...
}
};
struct Derived : Base<Derived>
{
void implementation();
};
会object.interface()
产生与调用相同的性能的调用object.implementation()