1. 场景
我想要编写一个函数,利用模板来返回特定的值,比如这里创建布局,但是返回的时候,可能是QWidget,也可能是QVBoxLayout,就想到了使用模板参数。
#define is_same(Class,Obj) std::is_base_of<Class, Obj>::value
template <typename Type, typename T>
Type* createStandardLayout(const QString& name, T* widget_container)
{
QLabel* lab_tip = new QLabel(name);
lab_tip->setProperty("tip", true);
lab_tip->setFixedHeight(40);
QVBoxLayout* widget_layout = addVLayout(lab_tip, widget_container);
widget_layout->setContentsMargins(10, 0, 0, 0);
if (is_same(QWidget, Type))
{
QWidget* wid = new QWidget;
wid->setLayout(widget_layout);
return static_cast<Type*>(wid);
}
else if (is_same(QVBoxLayout, Type))
{
return static_cast<Type*>(widget_layout);
}
else
{
static_assert(is_same(QWidget, Type) || is_same(QVBoxLayout, Type),
"Type must be QWidget or QVBoxLayout");
return nullptr;
}
}
你会发现上面的程序会报错下面的信息:
不使用 constexpr 时,编译器会尝试编译所有的分支代码,即使这些分支在运行时条件不满足的情况下不会被执行。这导致编译器在遇到不合法的类型转换时(例如尝试将 QVBoxLayout* 转换为 QWidget*)报错,因为这些转换在语法上是不合法的。
if constexpr 在编译时就会对条件进行判断,只编译满足条件的分支代码,避免了不合法代码路径的编译。
2. 解决方法 1 - constexpr限定
template <typename Type, typename T>
Type* createStandardLayout(const QString& name, T* widget_container)
{
QLabel* lab_tip = new QLabel(name);
lab_tip->setProperty("tip", true);
lab_tip->setFixedHeight(40);
QVBoxLayout* widget_layout = addVLayout(lab_tip, widget_container);
widget_layout->setContentsMargins(10, 0, 0, 0);
if constexpr (is_same(QWidget, Type))
{
QWidget* wid = new QWidget;
wid->setLayout(widget_layout);
return static_cast<Type*>(wid);
}
else if constexpr(is_same(QVBoxLayout, Type))
{
return static_cast<Type*>(widget_layout);
}
else
{
static_assert(is_same(QWidget, Type) || is_same(QVBoxLayout, Type),
"Type must be QWidget or QVBoxLayout");
return nullptr;
}
}
3. 解决办法 2 - 模板特化
// 通用模板
template <typename Type, typename T>
inline Type* createStandardLayoutImpl(const QString& name, T* widget_container);
// QWidget 特化
template <typename T>
inline QWidget* createStandardLayoutImpl<QWidget, T>(const QString& name, T* widget_container) {
QLabel* lab_tip = new QLabel(name);
lab_tip->setProperty("tip", true);
lab_tip->setFixedHeight(40);
QVBoxLayout* widget_layout = addVLayout(lab_tip, widget_container);
widget_layout->setMargin(0);
QWidget* wid = new QWidget;
wid->setLayout(widget_layout);
return wid;
}
// QVBoxLayout 特化
template <typename T>
inline QVBoxLayout* createStandardLayoutImpl<QVBoxLayout, T>(const QString& name, T* widget_container) {
QLabel* lab_tip = new QLabel(name);
lab_tip->setProperty("tip", true);
lab_tip->setFixedHeight(40);
QVBoxLayout* widget_layout = addVLayout(lab_tip, widget_container);
widget_layout->setMargin(0);
return widget_layout;
}
// 主函数调用模板特化
template <typename Type, typename T>
inline Type* createStandardLayout(const QString& name, T* widget_container) {
return createStandardLayoutImpl<Type, T>(name, widget_container);
}