【发布时间】:2022-01-10 15:32:13
【问题描述】:
考虑这两个例子
示例 1
template<Type type>
static BaseSomething* createSomething();
template<>
BaseSomething* createSomething<Type::Something1>()
{
return Something1Creator.create();
}
template<>
BaseSomething* createSomething<Type::Something2>()
{
return Something2Creator.create();
}
.... // other somethings
示例2
template<Type type>
static BaseSomething* createSomething()
{
if constexpr(type == Type::Something1)
{
return Something1Creator.create();
}
else if constexpr(type == Type::Something2)
{
return Something2Creator.create();
}
// Other somethings
}
我知道这两个示例在概念上是相同的,但考虑到这些函数位于 SomethingFactory.hpp 文件中,而我的 main.cpp 包含它。
在main.cpp 中,我可能只创建Something1 类型而不知道存在其他Something 类型。
最后我真的关心我的可执行文件的大小。您认为我应该采用哪种模式使我的可执行文件最小化?或者这些没什么大不了的,反正我们都注定要失败?
【问题讨论】:
标签: c++ templates compile-time specialization if-constexpr