Skip to content

Commit

Permalink
Update null.md
Browse files Browse the repository at this point in the history
NULLの定義としてnullptrが適合することを記載
  • Loading branch information
tetsurom committed Oct 12, 2022
1 parent 5d25aa9 commit a4d88a8
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion reference/cstddef/null.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
## 備考
C++における「ヌルポインタ定数」の定義により、マクロ`NULL`の値として`0`や`0L`は規格に適合する定義だが、`(void*)0`は不正である
C++03において、「ヌルポインタ定数」は「値が0の整数リテラル」と定義されていた。したがって、マクロ`NULL`の値として`0`や`0L`は規格に適合する定義だが、`(void*)0`はリテラルではなく式であるため不正である

This comment has been minimized.

Copy link
@k-satoda

k-satoda Oct 13, 2022

Contributor

そうだっけ?と思って C++03 の規定を確認すると以下のようになっていました。

4.10 [conv.ptr] p1

A null pointer constant is an integral constant expression (5.19) rvalue
of integer type that evaluates to zero. ...

5.19 [expr.const] p1

An integral constant-expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5),
non-type template parameters of integral or enumeration types, and
sizeof expressions. Floating literals (2.13.3) can appear only if ...

これらによるとリテラル以外を含むこともできるので、
(void*)0はリテラルではなく式であるため不正」という
追加された理由付けは正しくないということになりそうです。
nullptr_t 以外のヌルポインタ定数がリテラルに限定されたのは
C++14 cplusplus/draft@878a6dc でした。)

正確になるように文量を増やすことも考えられますが、ログで挙げられた目的
「nullptrが適合することを記載」だけなら詳細な定義に触れる必要は無さそう
なので削る方向のほうが簡単でいいかなと思いました。

検討ください。

This comment has been minimized.

Copy link
@k-satoda

k-satoda Jan 7, 2023

Contributor

@tetsurom 遅くなっちゃいましたが↑の件、情報量を維持する方向で考え直して
533c542 で修正を入れました。

This comment has been minimized.

Copy link
@tetsurom

tetsurom Jan 7, 2023

Author Contributor

ありがとうございます🙇
最初の投稿に気がついていませんでした……お待たせしてすみません。

This comment has been minimized.

Copy link
@k-satoda

k-satoda Jan 7, 2023

Contributor

おや。と思って見直すと最初の投稿で僕がちゃんとメンション付けてなかったせいな気もしてきました。
気を付けます。

C++11では、「ヌルポインタ定数」の定義に「[`std::nullptr_t`](/reference/cstddef/nullptr_t.md)型のprvalue」が追加されたため、`nullptr`も規格に適合する。しかし、`NULL`の値の型が変わるとコードの互換性を損なうことから、当面の間 `NULL` の定義は整数リテラルであると思われる。C++11以降は`NULL`ではなく[`nullptr`](/lang/cpp11/nullptr.md)を使用するとよい。
## 関連項目
- [C++11 `nullptr`](/lang/cpp11/nullptr.md)

0 comments on commit a4d88a8

Please sign in to comment.