假设我有一个名为loadData()的函数,它接受一个容器(用数据填充)和一个CSV文件。我需要以下超载:
loadData(std::vector<double>& data, const std::string& file);loadData(QVector<double>& data, const std::string& file);loadData(std::vector<std::complex<double>>& data, const std::string& file);loadData(QVector<std::complex<double>>& data, const std::string& file);loadData(std::vector<std::vector<double>>& data, const std::string& file);loadData(QVector<QVector<double>>& data, const std::string& file);loadData(std::vector<std::vector<std::complex<double>>>& data, const std::string& file);loadData(QVector<QVector<std::complex<double>>>& data, const std::string& file);QVector是Qt的类向量类,具有类似的API,但只使用一个模板参数T (而不是两个模板参数,如std::vector<T, A>)。
1-4的实现几乎是相同的(它只调用5-8,将单个向量包装在另一个向量中(类型相同!)。
5-8的实现是相同的(它只是调用CSV解析函数的适当重载)。
我还可能需要添加float重载或QVector<std::vector<...>> / std::vector<QVector<...>>重载。
基本上,这是一组巨大的过载,我想概括一下。
是否有可能将它们合并成两个函数模板(一个用于1D容器,另一个用于2D容器)?
谢谢
发布于 2018-05-08 12:26:21
是否有可能将它们合并成两个函数模板(一个用于1D容器,另一个用于2D容器)?
是的..。需要一点工作,但相对来说很简单。
也许有更简单的方法,但我建议创建一个自定义类型特征,以验证模板模板是std::vector还是QVector。
template <template <typename...> class>
struct isVector : public std::false_type
{ };
template <>
struct isVector<std::vector> : public std::true_type
{ };
template <>
struct isVector<QVector> : public std::true_type
{ };接下来是另一个自定义类型特征,以验证该类型是否为std::complex<T>类型的浮点类型。
template <typename T>
struct isCFloating : public std::is_floating_point<T>
{ };
template <typename T>
struct isCFloating<std::complex<T>> : public std::true_type
{ };现在您可以编写向量版本(截取也混合了std::vector/QVector情况),如下所示
template <template <typename ...> class V1,
template <typename ...> class V2, typename T>
auto loadData (V1<V2<T>> & v, std::string fn)
-> std::enable_if_t< isVector<V1>::value
&& isVector<V2>::value
&& isCFloating<T>::value>
{
std::cout << "- vector of vector version, " << fn << std::endl;
}之后,简单的向量版本(包装第一个参数并调用矢量版本)变成了
template <template <typename ...> class V, typename T>
auto loadData (V<T> & v, std::string fn)
-> std::enable_if_t<isVector<V>::value && isCFloating<T>::value>
{
std::cout << "- vector version, " << fn << std::endl;
V<V<T>> vv{1, v};
loadData(vv, fn);
}我已经准备了以下完整的示例,但是(对不起)目前我还没有QT平台,所以我添加了一个假的QVector
#include <vector>
#include <complex>
#include <iostream>
#include <type_traits>
// fake QVector
template <typename>
struct QVector
{
template <typename ... Ts>
QVector (Ts const & ...)
{ }
};
template <template <typename...> class>
struct isVector : public std::false_type
{ };
template <>
struct isVector<std::vector> : public std::true_type
{ };
template <>
struct isVector<QVector> : public std::true_type
{ };
template <typename T>
struct isCFloating : public std::is_floating_point<T>
{ };
template <typename T>
struct isCFloating<std::complex<T>> : public std::true_type
{ };
template <template <typename ...> class V1,
template <typename ...> class V2, typename T>
auto loadData (V1<V2<T>> & v, std::string fn)
-> std::enable_if_t< isVector<V1>::value
&& isVector<V2>::value
&& isCFloating<T>::value>
{
std::cout << "- vector of vector version, " << fn << std::endl;
}
template <template <typename ...> class V, typename T>
auto loadData (V<T> & v, std::string fn)
-> std::enable_if_t<isVector<V>::value && isCFloating<T>::value>
{
std::cout << "- vector version, " << fn << std::endl;
V<V<T>> vv{1, v};
loadData(vv, fn);
}
int main ()
{
std::vector<float> vf;
std::vector<std::complex<float>> vc;
std::vector<std::vector<double>> vvf;
std::vector<std::vector<std::complex<double>>> vvc;
QVector<long double> qf;
QVector<std::complex<long double>> qc;
QVector<QVector<float>> qqf;
QVector<QVector<std::complex<float>>> qqc;
loadData(vf, "case 1");
loadData(qf, "case 2");
loadData(vc, "case 3");
loadData(qc, "case 4");
loadData(vvf, "case 5");
loadData(qqf, "case 6");
loadData(vvc, "case 7");
loadData(qqc, "case 8");
// extra cases: mixing std::vector and QVector
std::vector<QVector<double>> vqf;
std::vector<QVector<std::complex<double>>> vqc;
QVector<std::vector<long double>> qvf;
QVector<std::vector<std::complex<long double>>> qvc;
loadData(vqf, "case 9");
loadData(vqc, "case 10");
loadData(qvf, "case 11");
loadData(qvc, "case 12");
}发布于 2018-05-08 11:06:54
您可以使用模板和基于迭代器的泛型算法来减少代码重复。例如:
enum class load_data_status
{
success
// something you need
};
template<typename I>
void loadData(const I& first,const I& last, std::ostream& file)
{
typedef typename std::iterator_traits<I>::value_type T;
// do load raw data
std::for_each(first, last, [file](const T& t) {
// do something
} );
}
template<typename I>
void loadComplexData(const I& first,const I& last, std::ostream& file)
{
typedef typename std::iterator_traits<I>::value_type C;
typedef typename C::value_type T;
// do load complex data
std::for_each(first, last, [file](const C& t) {
// do something
} );
}
template<typename T>
load_data_status loadData(const std::vector< std::vector<T> >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.cbegin(), data.cend(), [f] (const std::vector<T>& v) {
loadData(v.cbegin(), v.cend(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const QVector< QVector<T> >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.begin(), data.end(), [f] (const QVector<T>& v) {
loadData(v.begin(), v.end(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const std::vector< std::vector<std::complex<T> > >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.cbegin(), data.cend(), [f] (const std::vector<std::complex<T> >& v) {
loadComplexData(v.cbegin(), v.cend(), f);
} );
return load_data_status::success;
}
template<typename T>
load_data_status loadData(const QVector< QVector< std::complex<T> > >& data, const std::string& file) {
std::ofstream f(file);
std::for_each(data.begin(), data.end(), [f] (const QVector< std::complex<T> >& v) {
loadComplexData(v.begin(), v.end(), f);
} );
return load_data_status::success;
} https://stackoverflow.com/questions/50231612
复制相似问题