1

I am trying to combine different number of vector<double> variables into a vector< vector<double> >. I try to use cstdarg library. It throws out

error: cannot receive objects of non-trivially-copyable type ‘class myvectortype’ through ‘...’;

where

typedef vector< double > myvectortype; typedef vector< myvectortype > datavectortype;

Here is the definition of the function

datavectortype ExtractData::GetPixelData(int num, ...)
{
        datavectortype data_temp;
        va_list arguments;
        va_start (arguments, num);
        for(int i = 0; i<num; i++)
        {
                data_temp.push_back(va_arg ( arguments, myvectortype));
        }
        va_end ( arguments );
        return data_temp;
}

What could be done to fix this, thanks.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
bariskand
  • 31
  • 4
  • 1
    possible duplicate of [Variable number of arguments in C++?](http://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c) – Sadique Dec 23 '14 at 06:39
  • 1
    @bariskand: If all you are doing is pushing them back into a vector of vectors (you should use `std::move`, by the way) then you don't need a function for this. Just put them in curly braces (i.e. form an `initializer_list`.) Like this: `datavectortype vov = {v0, v1, v2, v3, v4, v5, v6};`. – yzt Dec 23 '14 at 06:44
  • OP, and don't forget to include the header utility if you are to use std::move – wingerse Dec 23 '14 at 06:51
  • @al-Acme, I am aware of that post, however I could not I understand how to handle it with vector structure, example with initialize_list is obscure to me. – bariskand Dec 23 '14 at 06:58
  • Take a vector of vectors of double – M.M Dec 23 '14 at 07:10
  • 2
    This seems like an XY problem – M.M Dec 23 '14 at 07:12

1 Answers1

3

Since C++11, you already just could do

std::vector<double> v1{1}, v2{2}, v3{3, 4};
std::vector<std::vector<double>> v {v1, v2, v3};

But if you want to do a function for that, you may use variadic template:

template <typename T, typename ...Ts>
std::vector<T> make_vector(const T& arg, const Ts&... args)
{
    return {arg, args...};
}

and so use it like:

std::vector<double> v1{1}, v2{2}, v3{3, 4};
std::vector<std::vector<double>> v = make_vector(v1, v2, v3);
Jarod42
  • 203,559
  • 14
  • 181
  • 302