Variadic template or initializer_list?

I want to initialise my class as an array and i want to do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <initializer_list>

template <class T> class test {
  private
  T* arr[][2];
  unsigned int rows;
  unsigned int cols;
  public
  test<T>(//What to write here??);
};

test<T>::test(//What to write here??) {
//rows = sizeof...(T); problem is i need the rows und cols
// i found this sizeof...(T) if i wanted to use variadic templates, but i have no clue how
// i also tried using initializer_list but then i need something,
//like (initializer_list< initializer_list<T> > args) ??
}

int main() {
    test<int> {{1,2},{3,4}}; // should be the input
}


As stated in the code i tried different kind of initializing but i'm not sure how to do it the best way and also i could not get it to work properly. Would be very nice if someone could enlighten me.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

class Test
{
public:
        Test(const std::vector<std::vector<int>>& in) :
                data(in)
        {
        }

private:
        std::vector<std::vector<int>> data;
};

int main()
{
        std::vector<std::vector<int>> data = {{1, 3}, {4, 6}};
        Test test = data;

        Test tes1({{1, 3}, {4, 6}});
}
1) Line 5 is invalid. Use containers to store data.
2)
i also tried using initializer_list but then i need something,
//like (initializer_list< initializer_list<T> > args) ??
Why not?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <initializer_list>
#include <vector>
#include <iostream>

template <class T> class test 
{
private:
  unsigned int rows;
  unsigned int cols;
public:
  std::vector<std::vector<T>> arr; 
  test(std::initializer_list<std::initializer_list<T>> r) : arr(r.begin(), r.end())
  {}
};

int main()
{
    test<int> a = {{1, 2, 3}, {4, 5, 6}};
    for(const auto& v: a.arr) {
        for(auto i: v)
            std::cout << i << ' ';
        std::cout << '\n';
    }
}

First of all thanks for the help.

The problem is this is a minimal example. I cannot use vector because the class is in a much larger bib i need to use and i cannot change the whole thing just to use vectors. :/ It would be so much easier.

*edit* Nevermind i found the answer. (http://stackoverflow.com/questions/15810171/is-there-a-way-to-pass-nested-initializer-lists-in-c11-to-construct-a-2d-matri)
Last edited on
Then you ar in the world of pain and manual memory management. I suggest to emulate 2D arrays with 1d array and use unique_ptr to hold it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <initializer_list>
#include <algorithm>
#include <iostream>
#include <memory>

template <class T> class test
{
private:
    std::unique_ptr<T[]> arr;
public:
    std::size_t rows;
    std::size_t cols;
    test(std::initializer_list<std::initializer_list<T>> r) : rows(r.size())
    {
        using init = const std::initializer_list<T>;
        cols = std::max_element(r.begin(), r.end(), [](init& lhs, init& rhs)
                                                    {return lhs.size() < rhs.size();}
                               )->size();
        arr = std::unique_ptr<T[]>(new T[rows*cols]);
        auto source = r.begin();
        auto dest = &arr[0];
        do  {
            std::copy(source->begin(), source->end(), dest);
            dest += cols;
        } while(++source != r.end());
    }
    T* operator[](std::size_t index)
    {
        return &arr[0] + index*cols;
    }
};

int main()
{
    test<int> a = {{1, 2, 3}, {4, 5, 6}};
    for(std::size_t i = 0; i < a.rows; ++i) {
        for(std::size_t j = 0; j < a.cols; ++j)
            std::cout << a[i][j] << ' ';
        std::cout << '\n';
    }
}
1 2 3
4 5 6
http://coliru.stacked-crooked.com/a/b28bee415aae8688
http://ideone.com/8ZRNSD
http://melpon.org/wandbox/permlink/uBeTGquJ9QjxCi0f
Last edited on
@MiiNiPaa: Thanks so much for your help. I did not know about the function unique_ptr. That was exactly what i was looking for.
Topic archived. No new replies allowed.