c++/qt :利用模板参数来返回不同类型的对象

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值