yohhoyの日記

技術的メモをしていきたい日記

ヌルポインタ定数と値0

C++14ではヌルポインタ定数の定義における値0の扱いが微妙に変更される。CWG defect #903の適用。

  • C++11以前:値0に評価される整数型の定数式(=計算結果が0
  • C++14:値0の整数リテラル(=コード上で0を明記)

下記コードにおいて、C++11以前ではg<0>のみwell-formedとなり、それ以外(g<1>など)はill-formedとなる。C++14では非型テンプレートパラメータNへの指定値によらずill-formedとなる(一貫性向上)。

template <int N> void g() {
  int* p = N;  // ??
}
g<0>();  // C++11: OK / C++14: NG
g<1>();  // C++11/14: NG

C++11以前

N3337(C++11) 4.10/p1より該当箇所を引用(下線部は強調)。*1

A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. (snip)

C++14

N3937(C++14 DIS) 4.10/p1より該当箇所を引用(下線部は強調)。

A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. (snip)

関連URL

*1:ISO C++03: "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero."