Tag: 统一初始化

为什么不emplace_back()使用统一的初始化?

以下代码: #include <vector> struct S { int x, y; }; int main() { std::vector<S> v; v.emplace_back(0, 0); } 使用GCC编译时出现以下错误: 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, […]

C ++标准是否保证统一初始化是exception安全的?

#include <iostream> using namespace std; struct A { A() { cout << "A" << endl; } ~A() { cout << "~A" << endl; } }; A Ok() { return {}; } A NotOk() { throw "NotOk"; } struct B { A a1; A a2; }; void f(B) {} int main() { try { f({ Ok(), […]

为什么这个片段使用统一的初始化编译g ++ 4.6而不是g ++ 4.7?

请注意, 派生使用C ++ 11统一初始化语法来调用基类构造函数。 class base { protected: base() {} }; class derived : public base { public: derived() : base{} // <– Note the c++11 curly brace syntax // using uniform initialization. Change the // braces to () and it works. {} }; int main() { derived d1; return 0; } g ++ 4.6编译这个,但是g […]