Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 完美转发模板参数包以放置\u back-编译失败_Templates_C++11_C++14_Variadic Templates_Perfect Forwarding - Fatal编程技术网

Templates 完美转发模板参数包以放置\u back-编译失败

Templates 完美转发模板参数包以放置\u back-编译失败,templates,c++11,c++14,variadic-templates,perfect-forwarding,Templates,C++11,C++14,Variadic Templates,Perfect Forwarding,抱歉,如果有类似问题的解决方案,我无法找到具有类似错误输出/问题的解决方案 我正在尝试编译以下代码: #include <vector> using namespace std; template< typename value_type > class obj { vector<value_type> m_v; public: template<class... vaList_t> void _emplace_ba

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

我正在尝试编译以下代码:

#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;
}
#包括
使用名称空间std;
模板<类型名称值\u类型>
obj类
{
向量m_v;
公众:
模板
无效放置返回(有效值和…i_值)
{
m_v.向后安放(向前(i_值)…);
}
};
int main()
{
客观事物;
事物。安置(1,2,3,4);
返回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)...); }
在/usr/include/x86_64-linux-gnu/c++/4.9/bits/c++allocator.h:33:0中包含的文件中,
从/usr/include/c++/4.9/bits/allocator.h:46,
从/usr/include/c++/4.9/vector:61,
从1开始:
/usr/include/c++/4.9/ext/new_allocator.h:在'void\u gnu\u cxx::new_allocator::construct(_Up*,_Args&&&…)[with _Up=int;_Args={int,int,int,int};_Tp=int]的实例化中:
/usr/include/c++/4.9/bits/alloc_traits.h:253:4:static std::_requirestd::allocator_traits::_S_构造(_alloc&,_Tp*,_Args&……[with _Tp=int;_Args={int,int,int,int};_alloc=std::allocator;std:_Require=void]'
/usr/include/c++/4.9/bits/alloc_traits.h:399:57:static decltype(__a,__p,(forward)(std::allocator_traits::construct::__args))std::allocator_traits::construct(_-alloc,_-Tp*,_-args&&…[with _-Tp=int;_-args={,int,int,int};_-alloc=std::分配器;decype(_-S,_-Tp*,)向前)(std::allocator_traits::construct::__args)…)=]'
/usr/include/c++/4.9/bits/vector.tcc:97:40:void std::vector::emplace_back(_Args&&…[带_Args={int,int,int,int};_Tp=int;_Alloc=std::allocator]中的必填项
13:3:从“void obj::_emplace_back(vaList_t&&…[带vaList_t={int,int,int,int};value_type=int]开始需要”
20:30:从这里开始需要
/usr/include/c++/4.9/ext/new_allocator.h:120:4:错误:新初始值设定项表达式列表被视为复合表达式[-fppermissive]
{::新建((void*)uu p)u向上(std::转发(u参数));}
问题:

  • 对于完美转发模板参数包,这种语法正确吗?(如果不正确,是什么?)
  • 实现这种行为的正确方法是什么
先谢谢你

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

template< class... Args > void emplace_back( Args&&... args );
templatevoid emplace\u back(Args&&…Args);
具有完美的转发模板参数包,其中包含多个要添加的元素

例如:

  • “1,2,3,4”-通过 后置
  • “真、假、假”-通过emplace_back向向量背面添加3个不同的对象

c.emplace\u back
c
中创建一个元素。如果需要多个元素,则需要将
emplace\u 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)... };
}
模板
无效放置返回(有效值和…i_值)
{
int dummy[]={0,(m_v.emplace_back(向前(i_值)),0)…};
}

也许你的意思是
m_v.insert(m_v.end(),{forward(i_值)…})
,或者
(m_v.emplace_back(forward(i_值)),…);
你想在向量中插入4项吗?@PiotrSkotnicki the“,…”不编译。insert确实编译。但是它不能完美地转发参数包?insert的函数参数是迭代器和初始化列表,因此会产生额外的开销?@kennytm Nope,我想使用内部对象的(此案例向量)emplace\u back函数用于放置已从模板参数包转发的元素。@quantumkall但您不能用
int,int,int,int
构造
int
。value\u type需要接受
int,int,int,int
作为此编译的参数,例如
value\u type==std::tuple
。谢谢。我看不出来w:“将新元素附加到容器的末尾。”(模板参数版本适用于具有多个参数的对象,请注意多个对象)。@Quantumkal正是如此。
emplace\u back
over
push\u back
的真正威力在于它允许您直接在容器中构造元素(而不是复制/移动它)通过向下传递构造函数参数。看起来这是在使用逗号运算符使模板返回扩展的副作用?丑陋但有用:)。