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
スローしない場合、new 式はスローできますか?
つまり、次のnoexcept
仕様は正しく安全ですか?
Obj(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...))) {
new (&value_pod_) T(std::forward<Args>(args)...);
}