可以使用函数模板的特化来实现只使用一个函数模板返回Qvector和std::vector。

示例代码如下:

#include <vector>
#include <QVector>

template <typename T>
struct VectorType {};

template <>
struct VectorType<std::vector<int>> {
    using type = std::vector<int>;
};

template <>
struct VectorType<QVector<int>> {
    using type = QVector<int>;
};

template <typename T>
typename VectorType<T>::type getVector() {
    return typename VectorType<T>::type();
}

int main() {
    auto v1 = getVector<std::vector<int>>(); // v1 is std::vector<int>
    auto v2 = getVector<QVector<int>>(); // v2 is QVector<int>
    return 0;
}

在这里,我们使用了一个VectorType结构体来映射不同类型的向量容器类型。然后,我们使用函数模板特化来为每个容器类型指定其对应的VectorType。最后,我们使用typename VectorType<T>::type来返回容器类型的实际类型

c++ 只用一个函数模板返回值是是QvectorT、stdvectorT怎么写?

原文地址: https://www.cveoy.top/t/topic/cvEK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录