निम्नलिखित कोड: जब जीसीसी के साथ संकलितemplace_back() समान प्रारंभिकरण का उपयोग क्यों नहीं करता है?
#include <vector>
struct S
{
int x, y;
};
int main()
{
std::vector<S> v;
v.emplace_back(0, 0);
}
देता है निम्न त्रुटियों:
In file included from c++/4.7.0/i686-pc-linux-gnu/bits/c++allocator.h:34:0,
from c++/4.7.0/bits/allocator.h:48,
from c++/4.7.0/vector:62,
from test.cpp:1:
c++/4.7.0/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = S; _Args = {int, int}; _Tp = S]':
c++/4.7.0/bits/alloc_traits.h:265: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 = S; _Args = {int, int}; _Alloc = std::allocator<S>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]'
c++/4.7.0/bits/alloc_traits.h:402:4: required from 'static void std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = S; _Args = {int, int}; _Alloc = std::allocator<S>]'
c++/4.7.0/bits/vector.tcc:97:6: required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int, int}; _Tp = S; _Alloc = std::allocator<S>]'
test.cpp:11:24: required from here
c++/4.7.0/ext/new_allocator.h:110:4: error: new initializer expression list treated as compound expression [-fpermissive]
c++/4.7.0/ext/new_allocator.h:110:4: error: no matching function for call to 'S::S(int)'
c++/4.7.0/ext/new_allocator.h:110:4: note: candidates are:
test.cpp:3:8: note: S::S()
test.cpp:3:8: note: candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr S::S(const S&)
test.cpp:3:8: note: no known conversion for argument 1 from 'int' to 'const S&'
test.cpp:3:8: note: constexpr S::S(S&&)
test.cpp:3:8: note: no known conversion for argument 1 from 'int' to 'S&&'
सुझाव है कि vector
नियमित ()
निर्माता सिंटैक्स का उपयोग कर रहा है emplace_back()
को तर्क से तत्व के निर्माण के लिए। उपरोक्त काम जैसे उदाहरण बनाने के लिए vector
{}
वर्दी-प्रारंभिक वाक्यविन्यास का उपयोग क्यों नहीं करता है?
ऐसा लगता है कि {}
का उपयोग करके खोने के लिए कुछ भी नहीं है (यह एक होने पर कन्स्ट्रक्टर को कॉल करता है, लेकिन तब भी काम करता है जब कोई नहीं होता), और यह सी ++ की भावना में अधिक होगा 11 {}
का उपयोग करने के लिए - सभी के बाद, वर्दी प्रारंभिकरण का पूरा बिंदु यह है कि इसका उपयोग समान रूप से किया जाता है - अर्थात, हर जगह - वस्तुओं को प्रारंभ करने के लिए।
कोई भी सिंटैक्टिक ब्रेसिज़ इनिट सूचियों को पूरी तरह से आगे बढ़ाने के लिए एक भाषा supporrt प्रकार का आविष्कार कर सकता है। फिर कोई सिर्फ emplace_back ({a, b, ...}) –
@ जोहान्सचैब-लिटब: IMHO कह सकता है, यह 'std :: startizer_list' जैसा होना चाहिए था। लेकिन 'std :: आवंटक' के लिए सरल संशोधन चीजों को ठीक से ठीक करना चाहिए। – Potatoswatter
क्या होगा यदि theuser एक कस्टम आवंटन के साथ एक वेक्टर को प्रतिस्थापित करना चाहता है और emplace_back (1,2, alloc) लिखता है। इसे ({1,2}, आवंटन) के रूप में अग्रेषित करने के बारे में कैसे पता चलता है? आईएमओ कि आवंटक हैक चीज़ों ecen अधिक तोड़ता है। एक उचित समाधान पर काम किया जाना चाहिए। –