模板结构的支撑封闭的初始化列表

发布于 2025-01-26 20:59:31 字数 2166 浏览 3 评论 0原文

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

我在这里有点损失。错误在emplace_back行上,不知道如何解决。感谢任何帮助或提示,都将不胜感激。

编辑

GCC版本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

问题是结构初始化= {0}emplace_back

#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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2025-02-02 20:59:31

emplace_back()使用模板参数扣除来确定传递给该功能的元素的类型。括号封闭的初始化器列表不是一个表达式,也没有类型,因此不能由模板推导。您必须在此处明确调用构造函数:

items.emplace_back(item_t<3>{{1,2,3}});

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}});
一向肩并 2025-02-02 20:59:31

这里有两个问题:

尝试启动类型t的对象,例如t {...}被称为汇总初始化。在某些情况下,即使您没有接受initializer_list的构造函数,也有一个默认行为。在C ++ 11中,您不允许您提供非默认构造函数或课堂初始化器。因此,鉴于此定义,

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

您不能编写item_t&lt; 3&gt; t {1,2,3};

但是,那不是您的问题。您的代码失败的原因是emplace_back试图将参数转发给vector基础类型的构造函数。就您而言,没有匹配。请注意,不错的支撑列表不等于initializer_list在此上下文中,您无法通过添加inditize> inditioner_list构造器来解决此问题,并且必须帮助编译器其他方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文