C ++ 11 emplace_back on vector< struct&gt ;? [英] C++11 emplace_back on vector<struct>?

查看:532
本文介绍了C ++ 11 emplace_back on vector< struct&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

  #include< string& 
#include< vector>

using namespace std;

struct T
{
int a;
double b;
string c;
};

矢量< T> V;

int main()
{
V.emplace_back(42,3.14,foo);
}

不起作用:

  $ g ++ -std = gnu ++ 11 ./test.cpp 
在从/usr/include/c++/4.7/x86_64-linux-gnu /bits/c++allocator.h:34:0,
来自/usr/include/c++/4.7/bits/allocator.h:48,
来自/usr/include/c++/4.7/ string:43,
from ./test.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h:在'void __gnu_cxx :: new_allocator< _Tp> ::构造(_Up *,_Args&& ...)[with _Up = T; _Args = {int,double,const char(&)[4]}; _Tp = T]':
/usr/include/c++/4.7/bits/alloc_traits.h:253:4:需要从'static typename std :: enable_if< std :: allocator_traits< _Alloc> :: __ construct_helper& _Tp,_Args> :: value,void> :: type std :: allocator_traits< _Alloc> :: _ S_construct(_Alloc& _Tp *,_Args& _Args = {int,double,const char(&)[4]}; _Alloc = std :: allocator< T> ;; typename std :: enable_if< std :: allocator_traits< _Alloc> :: __ construct_helper< _Tp,_Args> :: value,void> :: type = void]'
/usr/include/c++/4.7/bits/alloc_traits .h:390:4:需要从'static void std :: allocator_traits< _Alloc> :: construct(_Alloc& _Tp *,_Args&& ...)[with _Tp = T; _Args = {int,double,const char(&)[4]}; _Alloc = std :: allocator< T>]'
/usr/include/c++/4.7/bits/vector.tcc:97:6:需要从'void std :: vector< _Tp,_Alloc> :: emplace_back (_Args& ...)[with _Args = {int,double,const char(&)[4]}; _Tp = T; _Alloc = std :: allocator< T>]'
./test.cpp:17:32:需要从这里
/usr/include/c++/4.7/ext/new_allocator.h:110:4 :error:没有匹配函数调用'T :: T(int,double,const char [4])'
/usr/include/c++/4.7/ext/new_allocator.h:110:4:note :candidate are:
./test.cpp:6:8:note:T :: T()
./test.cpp:6:8:note:candidate expects 0 arguments,3 provided
./test.cpp:6:8:note:T :: T(const T&)
./test.cpp:6:8:note:candidate expects 1 argument,3提供
./test.cpp:6:8:note:T :: T(T&&)
./test.cpp:6:8:note:candidate expects 1 argument,3提供

这样做的正确方法是什么,为什么?



$ c>语法以初始化新元素:

  V.emplace_back(T {42,3.14,foo}); 


Consider the following program:

#include <string>
#include <vector>

using namespace std;

struct T
{
    int a;
    double b;
    string c;
};

vector<T> V;

int main()
{
    V.emplace_back(42, 3.14, "foo");
}

It doesn't work:

$ g++ -std=gnu++11 ./test.cpp
In file included from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34:0,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/string:43,
                 from ./test.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = T; _Args = {int, double, const char (&)[4]}; _Tp = T]’:
/usr/include/c++/4.7/bits/alloc_traits.h:253:4:   required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = T; _Args = {int, double, const char (&)[4]}; _Alloc = std::allocator<T>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.7/bits/alloc_traits.h:390:4:   required from ‘static void std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = T; _Args = {int, double, const char (&)[4]}; _Alloc = std::allocator<T>]’
/usr/include/c++/4.7/bits/vector.tcc:97:6:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int, double, const char (&)[4]}; _Tp = T; _Alloc = std::allocator<T>]’
./test.cpp:17:32:   required from here
/usr/include/c++/4.7/ext/new_allocator.h:110:4: error: no matching function for call to ‘T::T(int, double, const char [4])’
/usr/include/c++/4.7/ext/new_allocator.h:110:4: note: candidates are:
./test.cpp:6:8: note: T::T()
./test.cpp:6:8: note:   candidate expects 0 arguments, 3 provided
./test.cpp:6:8: note: T::T(const T&)
./test.cpp:6:8: note:   candidate expects 1 argument, 3 provided
./test.cpp:6:8: note: T::T(T&&)
./test.cpp:6:8: note:   candidate expects 1 argument, 3 provided

What is the correct way to do this and why?

(Also tried single and double braces)

解决方案

You can use the {} syntax to initialize the new element:

V.emplace_back(T{42, 3.14, "foo"});

这篇关于C ++ 11 emplace_back on vector&lt; struct&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆