【发布时间】:2014-08-07 14:48:40
【问题描述】:
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 不分配任何空间,new 表达式可以如果T 的构造函数不抛出就抛出?
即以下noexcept 规范是否正确且安全?
Obj(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
new (&value_pod_) T(std::forward<Args>(args)...);
}
【问题讨论】:
-
所有placement-
operator new所做的只是返回它传递的指针,所以不,placement new 本身不能抛出。 -
@Simple: Placement new 当然可以抛出,但前提是正在构造的对象在其构造函数中抛出。
-
@Mankarse 对,
new表达式作为一个整体可以,但operator new不能。 -
我对整个表达式感兴趣
-
您应该编辑您的问题以澄清这一点。
标签: c++ exception c++11 placement-new noexcept