静态多态性策略和CRTP有什么区别
What is the difference between Strategy and CRTP for static polymorphism?
我想要一个接口,其中包含在编译时选择的多个可能的实现。我看到 CRTP 是实现此目的的首选习语。为什么?另一种选择是策略模式,但我在任何地方都没有看到提到这种技术:
template <class Impl>
class StrategyInterface
{
public:
void Interface() { impl.Implementation(); }
void BrokenInterface() { impl.BrokenImplementation(); }
private:
Impl impl;
};
class StrategyImplementation
{
public:
void Implementation() {}
};
template <class Impl>
class CrtpInterface
{
public:
void Interface() { static_cast<Impl*>(this)->Implementation(); }
void BrokenInterface() { static_cast<Impl*>(this)->BrokenImplementation(); }
};
class CrtpImplementation : public CrtpInterface<CrtpImplementation>
{
public:
void Implementation() {}
};
StrategyInterface<StrategyImplementation> str;
CrtpImplementation crtp;
不幸的是,无论哪种情况,编译器都不会捕获BrokenInterface
,除非我实际尝试使用它。策略变体对我来说似乎更好,因为它避免了丑陋的static_cast
并且它使用组合而不是继承。CRTP是否还允许该战略不允许?为什么主要使用 CRTP?
策略模式的通常实现与您的 CRTP 实现完全相同。基类定义某种算法,释放在派生类中实现的一些部分。
因此,CRTP 实现了策略模式。您的策略接口只是委托细节的实现,而不是策略模式的实现。
虽然您的两个实现都实现了相同的效果,但我更喜欢 CRTP,因为它将利用可能的空基类优化。
除了静态多态性之外,CRTP 还提供了覆盖基类函数的能力,因为它使用了继承机制。
template <class Impl>
class BaseInterface
{
void genericFunc() { some implementation; }
}
如果使用 CRTP,派生类可以选择覆盖 genericFunc() 作为"特殊"实现,以防 genericFunc() 不适合。策略模式将无法提供正常继承带来的功能。
策略模式的一个优点是,如果 BasedInterface 需要在 Impl 中使用依赖类型,这将比 CRTP 容易得多。
template <class Impl>
class BaseInterface
{
using SomeDerivedType = typename Impl::SomeType;
}
相关文章:
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- Qt:remove() 和 rmdir() 有什么区别
- 这 4 个 lambda 表达式之间有什么区别?
- 将向量作为类>(值)<向量启动和向量<类>[值]有什么区别
- typedef 枚举和枚举类有什么区别?
- &C::c 和 &(C::c) 有什么区别?
- ascii 和 unicode 在处理级别有什么区别吗?
- C 中的常量限定符和 C++ 中的常量限定符有什么区别?
- "ABC" 和 "ABC" ) 在C++中有什么区别?
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 引用捕获和在 lambda 中通过引用发送参数有什么区别 (C++)
- 两种访问I2C总线的方法有什么区别?
- 两种模板示例有什么区别?
- 这两种C++语法之间有什么区别?
- lua 5.0.2 模块和 5.3.5 有什么区别?
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- std::enable_if 和 std::enable_if_t 有什么区别?