bol*_*lov 6 c++ exception placement-new noexcept c++11
template <class T>
struct Obj {
  // Plain Old Data for T
  using InternalPod = typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type;
  InternalPod value_pod_;
  template<class... Args>
  Obj(Args&&... args) { // my constructor
    // placement new: construct the value in the statically allocated space
    new (&value_pod_) T(std::forward<Args>(args)...); // <- can this whole expression throw if the constructor of T doesn’t throw?
  }
}
 new如果分配失败或者构造失败,则抛出正常(如果有其他情况,则纠正我),但由于放置new不分配任何空间,如果构造函数T不抛出,新表达式是否可以抛出?
即以下noexcept规范是否正确且安全?
Obj(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
  new (&value_pod_) T(std::forward<Args>(args)...);
}
Die*_*ühl 11
放置new从<new>被宣告noexcept根据18.6 support.dynamic]段1:
...
void* operator new (std::size_t size, void* ptr) noexcept;...
使用new表达式时,系统完成两件事:
operator new()来获取内存.如果内存分配失败,它应该std::bad_alloc在operator new()没有noexcept资格的情况下抛出,nullptr否则返回.nullptr返回non ,则表达式将调用表达式中类型的构造函数new.如果此构造因异常而失败,则调用operator delete()匹配的调用operator new()结果operator new().由于内存分配不会失败,因此获取异常的唯一选择是来自类型的构造函数.
| 归档时间: | 
 | 
| 查看次数: | 1033 次 | 
| 最近记录: |