A function template that accepts both std::vector and QVector?

纵饮孤独 提交于 2020-01-04 05:24:11

问题


Suppose I have a function called loadData() which takes a container (to be filled with data) and a CSV file. I need the following overloads:

  1. loadData(std::vector<double>& data, const std::string& file);
  2. loadData(QVector<double>& data, const std::string& file);
  3. loadData(std::vector<std::complex<double>>& data, const std::string& file);
  4. loadData(QVector<std::complex<double>>& data, const std::string& file);
  5. loadData(std::vector<std::vector<double>>& data, const std::string& file);
  6. loadData(QVector<QVector<double>>& data, const std::string& file);
  7. loadData(std::vector<std::vector<std::complex<double>>>& data, const std::string& file);
  8. loadData(QVector<QVector<std::complex<double>>>& data, const std::string& file);

QVector is Qt's vector-like class with a similar API, but taking only one template argument T (instead of two, like std::vector<T, A>).

The implementation for 1-4 is almost the same (it just calls 5-8, wrapping that single vector in another vector (of the same type!).

The implementation for 5-8 is the same (it's just calling the appropriate overload of the CSV parsing function).

I may also need to add float overloads, or QVector<std::vector<...>> / std::vector<QVector<...>> overloads.

Basically, it's a huge set of overloads that I'd like to generalize.

Is it possible to combine all of them into maybe 2 function templates (one for 1D containers, another for 2D containers)?

Thanks


回答1:


Is it possible to combine all of them into maybe 2 function templates (one for 1D containers, another for 2D containers)?

Yes... require a little work but is relatively simple.

Maybe there are simpler methods but I propose the create a custom type traits that verify if a template-template is std::vector or 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
 { };

Next another custom type traits to verify if a type is a floating point type of a std::complex<T> type

template <typename T>
struct isCFloating : public std::is_floating_point<T>
 { };

template <typename T>
struct isCFloating<std::complex<T>> : public std::true_type
 { };

Now you can write the vector-of-vector version (that intercept also mixed std::vector/QVector case) as follows

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;
 }

and, after, the simple vector version (that wrap the first argument and call the vector-of-vector version) become

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);
 }

I've prepared the following full working example but (sorry) I don't have a QT platform available at the moment so I have added a fake 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");
 }



回答2:


You can use templates and iterators based generic algorithms to reduce code duplication. For example:

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;
} 



回答3:


When writing generic code, pay particular attention to separating concerns.

There are two concerns in loadData :-

  • getting objects from a file
  • appending those objects to a container

So let's build a template for acting as the data source (which we can specialise based on the value type), and then write one overload per container type in terms of that.

First attempt:

#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <complex>

struct data_file_loader_base
{
    data_file_loader_base(std::string const& path)
    : input_stream_(path)
    , items_(get_input<std::size_t>())
    {

    }

    template<class T>
    T get_input()
    {
        T val;
        input_stream_ >> val;
        return val;
    }

    std::size_t items() const
    {
        return items_;
    }

    std::ifstream& input_stream()
    {
        return input_stream_;
    }

private:
    std::ifstream input_stream_;
    std::size_t items_;
};

// basic value loader
template<class Type>
struct data_file_loader
: data_file_loader_base
{
    data_file_loader(std::string const& path)
    : data_file_loader_base(path)
    {}

    std::istream_iterator<Type> begin()
    {
        return { input_stream() };
    }

    std::istream_iterator<Type> end()
    {
        return { };
    }
};

// specialize for types that need custom input
// in this case, for a complex. But could easily be a vector etc.
template<class Type>
struct data_file_loader<std::complex<Type>>
: data_file_loader_base
{
    data_file_loader(std::string const& path)
    : data_file_loader_base(path)
    {}

    struct iterator
    {
        using value_type = std::complex<Type>;
        using iterator_category = std::forward_iterator_tag;
        using pointer = value_type*;
        using reference = value_type&;
        using difference_type = int;

        iterator(data_file_loader& loader, bool eof = false)
        : self_(loader)
        , eof_(eof || check_eof())
        {
        }

        bool operator==(iterator const& other) const
        {
            return eof_ && other.eof_;
        }

        bool operator!=(iterator const& other) const
        {
            return !(*this == other);
        }

        iterator& operator++() {
            return *this;
        }

        value_type operator*() {
            auto result = value_type { self_.get_input<Type>(), self_.get_input<Type>() };
            eof_ = check_eof();
            return result;
        }

        bool check_eof() const {
            return !input_stream();
        }

        data_file_loader& self_;
        bool eof_ = false;
    };

    iterator begin()
    {
        return { *this, false };
    }

    iterator end()
    {
        return { *this, true };
    }
};


// one overload per container type
template<class Type>
void loadData(std::vector<Type>& target, std::string const& path)
{
    auto loader = data_file_loader<Type>(path);
    target.reserve(loader.items());
    std::copy(std::begin(loader), std::end(loader), std::back_inserter(target));
}

// test
int main()
{
    std::vector<int> vi;
    loadData(vi, "foo.txt");

    std::vector<std::complex<double>> vc;
    loadData(vc, "foo_complex.txt");
}


来源:https://stackoverflow.com/questions/50231612/a-function-template-that-accepts-both-stdvector-and-qvector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!