17

私が理解している限り(少なくとも について)、自明でない場合(暗黙的に生成されたまたは) c++14、デストラクタは存在できません。自明でないデストラクタを持つ構造体のコンストラクタを宣言するポイントは何ですか?constexpr=defaultconstexpr

struct X {
  int a_;

  constexpr X(int a) : a_{a} {}

  // constexpr ~X(){}; // Error dtor cannot be marked constexpr
  // ~X(){}; // causes  error at y declaration: temporary of non-literal type ‘X’
             // in a constant expression .
};

template <int N> struct Y {};

int main() {
  Y<X{3}.a_> y; // OK only if the destructor is trivial
  (void)y;
}
// tested with c++14 g++-5.1.0 and clang++ 3.5.0

たとえば、デストラクタが明らかに明示的に定義されていても、std::unique_ptrいくつかのコンストラクタ constexpr(デフォルトおよび)があります(オブジェクトがオブジェクトは空の状態にあり、私が見たように、空のデストラクタでさえオブジェクトをコンパイル定数式で使用することはできません)nullptr_tnullptr

もう 1 つの例は、std::variantの提案です。ほとんどすべてのコンストラクターconstexprがありますが、デストラクタには署名が~variant()あり、それを行う必要があります。call get<T_j> *this).T_j::~T_j() with j being index().

私は何が欠けていますか?

4

1 に答える 1