Brace-enclosed initializer list of templated struct
Asked Answered
S

2

4
#include <array>                                                                
#include <vector>                                                               
#include <cinttypes>                                                            
#include <iostream>                                                             

using namespace std;                                                            

template<size_t N>                                                              
struct item_t {                                                                 
  array<uint32_t, N> weight = {0};                                              
};                                                                              

int main(void) {                                                                

  vector<item_t<3>> items;                                                      
  items.emplace_back({{9,2,3}});                                                
  cout << items[0].weight[0] << endl;                                           
  return 0;                                                                     
};  

I'm at a bit of a loss here. Error is on the emplace_back line and no idea how to resolve it. Any help or hints would be appreciated, thanks.

EDIT

gcc version 4.8.2

$ g++ -std=c++11 test.cpp 
test.cpp: In function ‘int main()’:
test.cpp:16:30: error: no matching function for call to ‘std::vector<item_t<3ul> >::emplace_back(<brace-enclosed initializer list>)’
  items.emplace_back({{9,2,3}});
                              ^
test.cpp:16:30: note: candidate is:
In file included from /usr/include/c++/4.8/vector:69:0,
                 from test.cpp:2:
/usr/include/c++/4.8/bits/vector.tcc:91:7: note: void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = item_t<3ul>; _Alloc = std::allocator<item_t<3ul> >]
       vector<_Tp, _Alloc>::
       ^
/usr/include/c++/4.8/bits/vector.tcc:91:7: note:   candidate expects 0 arguments, 1 provided

The problem is with the struct initialization = {0} and with emplace_back.

Spindell answered 30/6, 2014 at 15:39 Comment(2)
what error do you get?Bergama
It can be simplified by removing the vector and just assigning a brace initializer to a item_t<3>.Spindell
F
7

emplace_back() uses template argument deduction to determine the types of the elements passed to the function. A brace enclosed initializer list is not an expression and doesn't have type and therefore cannot be deduced by the template. You have to explicitly call the constructor here:

items.emplace_back(item_t<3>{{1,2,3}});
Fuzee answered 30/6, 2014 at 15:54 Comment(1)
That helps with the emplace_back and works if remove the = {0}. Thanks.Spindell
O
3

There are two issues here :

Trying to init a object of type T like this T{...} is referred to as aggregate initialization. Under some conditions, there is a default behaviour specified for it, even if you don't have a constructor which accepts a initializer_list. In C++11, you are not allowed to provide non-default constructors or in-class initializers. So, given this definition

template<size_t N>                                                              
struct item_t {                                                                 
  array<uint32_t, N> weight   = {0};                                            
};

you cannot write item_t<3> t{1,2,3};.

That, however, isn't your problem. The reason your code fails is that emplace_back tries to forward the arguments to a constructor of the vectors underlying type. In your case, there isn't a match. Note that nice a braced-init list isn't equivalent to an initializer_list in this context, you cannot solve this problem by adding an initializer_list constructor and will have to help the compiler out some other way.

Oxbridge answered 30/6, 2014 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.