Placement new plus destructor and simple value initialization noexcept semantic

时间:2016-09-01 06:26:24

标签: c++ c++11 semantics typetraits noexcept

Can one assume in unevaluated context, that (::new (std::declval< void * >()) T())->~T() is semantically (in sense of noexcept, but not in sense of type of expression) equivalent to simple T()? Assume that neither global, nor class-scope operator new are overloaded, if it mutters much.

Often in type traits T() used inside operator noexcept() to determine whether only the separate constructor is noexcept or not. Surely it is wrong.

To prevent the loss of generality one can assume that T() here is either a calling of a default constructor or of any other constructor.

1 个答案:

答案 0 :(得分:0)

  

可以假设在未评估的上下文中,(::new (std::declval< void * >()) T())->~T()在语义上完全等同于简单   T()

不,最后一个表达式是析构函数,它隐式地是void返回类型。

#include <new>
#include <type_traits>

struct T{int x; double y; };
int main(){
    constexpr int size = sizeof((::new (std::declval< void * >()) T())->~T());
}

编译器应该抱怨试图让sizeof成为void