3

I have the code:

#include <initializer_list>
#include <vector>    

class Foo {
    std::vector<std::vector<int> > data;
    public:
    Foo(std::initializer_list<std::initializer_list<int> > & data) : data(data) {}
};

int main(){
    Foo f {{1,2},{3,3}};
    return 0;
}

And the compiler complains:

initalizer_list_test.cpp: In constructor ‘Foo::Foo(std::initializer_list<std::initializer_list<int> >&)’:
initalizer_list_test.cpp:11:79: error: no matching function for call to ‘std::vector<std::vector<int> >::vector(std::initializer_list<std::initializer_list<int> >&)’
     Foo(std::initializer_list<std::initializer_list<int> > & data) : data(data) {}
                                                                               ^
initalizer_list_test.cpp:11:79: note: candidates are:
In file included from /usr/include/c++/4.8.2/vector:64:0,
                 from initalizer_list_test.cpp:2:
/usr/include/c++/4.8.2/bits/stl_vector.h:392:9: note: template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&)
         vector(_InputIterator __first, _InputIterator __last,
         ^
/usr/include/c++/4.8.2/bits/stl_vector.h:392:9: note:   template argument deduction/substitution failed:
initalizer_list_test.cpp:11:79: note:   candidate expects 3 arguments, 1 provided
     Foo(std::initializer_list<std::initializer_list<int> > & data) : data(data) {}

How to get it work properly?

1
  • wouldn´t std::initializer_list<std::vector<std::vector<int>>> do?
    – JonPall
    Mar 2, 2014 at 21:45

1 Answer 1

4

First, just use an initializer list of vectors in order to initialize the 2D vector. Then just take it by value instead of by reference (initializer lists, to my knowledge, are normally passed by value and a non-const reference won't work):

Foo(std::initializer_list<std::vector<int> > data) : data(data) {}
10
  • Doesn't types have to match? How does the compiler know the difference between 1D and 2D with just a 1D-vector of int as initializer type?
    – JonPall
    Mar 2, 2014 at 21:48
  • 1
    @JonPall, Vectors can be initialized with initializer lists containing their T. Thus, {{1, 2}} matches an initializer list containing one vector of two elements, which can be used to initialize the member because the list contains std::vector<int>, just like the member wants.
    – chris
    Mar 2, 2014 at 21:49
  • It is still not working. Error: initalizer_list_test.cpp: In constructor ‘Foo::Foo(std::initializer_list<std::initializer_list<int> >)’: initalizer_list_test.cpp:11:77: error: no matching function for call to ‘std::vector<std::vector<int> >::vector(std::initializer_list<std::initializer_list<int> >&)’ Foo(std::initializer_list<std::initializer_list<int> > data) : data(data) {}
    – SwiftMango
    Mar 2, 2014 at 21:51
  • @texasbruce, Interesting, it worked when I tested it with Clang, but the error looks like you didn't change it to std::initializer_list<std::vector<int>> and instead still have an init list of init lists.
    – chris
    Mar 2, 2014 at 21:51
  • 2
    @texasbruce, Precisely, an init list of init lists. It should be an init list of vectors. The matching when calling works great.
    – chris
    Mar 2, 2014 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.