gpt4 book ai didi

templates - 完美转发模板参数包到emplace_back - 编译失败

转载 作者:行者123 更新时间:2023-12-02 08:20:08 28 4
gpt4 key购买 nike

很抱歉,如果有类似问题的解决方案,我无法找到具有类似错误输出/问题的解决方案。

我正在尝试编译这段代码:

#include <vector>
using namespace std;

template< typename value_type >
class obj
{
vector<value_type> m_v;
public:

template<class... vaList_t>
void _emplace_back(vaList_t&&... i_values)
{
m_v.emplace_back( forward<vaList_t>(i_values)... );
}
};

int main()
{
obj<int> thing;
thing._emplace_back(1,2,3,4);
return 0;
}

但是编译失败:

    In file included from /usr/include/x86_64-linux-gnu/c++/4.9/bits/c++allocator.h:33:0,
from /usr/include/c++/4.9/bits/allocator.h:46,
from /usr/include/c++/4.9/vector:61,
from 1:
/usr/include/c++/4.9/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = int; _Args = {int, int, int, int}; _Tp = int]':
/usr/include/c++/4.9/bits/alloc_traits.h:253:4: required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = int; _Args = {int, int, int, int}; _Alloc = std::allocator<int>; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]'
/usr/include/c++/4.9/bits/alloc_traits.h:399:57: required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = int; _Args = {int, int, int, int}; _Alloc = std::allocator<int>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
/usr/include/c++/4.9/bits/vector.tcc:97:40: required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int, int, int, int}; _Tp = int; _Alloc = std::allocator<int>]'
13:3: required from 'void obj<value_type>::_emplace_back(vaList_t&& ...) [with vaList_t = {int, int, int, int}; value_type = int]'
20:30: required from here
/usr/include/c++/4.9/ext/new_allocator.h:120:4: error: new initializer expression list treated as compound expression [-fpermissive]
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

问题:

  • 对于完美转发模板参数包,这在句法上是否正确? (如果不是什么是?)
  • 实现此行为的正确方法是什么?

提前谢谢你。

编辑抱歉有任何歧义。我正在尝试使用类型签名调用这个名为 emplace_back 的向量函数:

template< class... Args > void emplace_back( Args&&... args );

完美转发,模板参数包,包含多个待添加元素。

例如:

  • "1,2,3,4"- 通过将 4 个不同的对象添加到向量的后面emplace_back
  • "true,false,false"- 通过 emplace_back 添加 3 个不同的对象到向量的后面

最佳答案

c.emplace_backc 中创建一个单个 元素。如果您想要多个,则需要将 emplace_back 作为扩展的一部分。像这样:

template<class... vaList_t>
void _emplace_back(vaList_t&&... i_values)
{
int dummy[] = { 0, (m_v.emplace_back(forward<vaList_t>(i_values)), 0)... };
}

[Live example]

关于templates - 完美转发模板参数包到emplace_back - 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997318/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com