Skip to content

Commit

Permalink
C++17インライン変数 : 名前のないクラスオブジェクトをインライン指定しても同じ実体にはならないことを記載
Browse files Browse the repository at this point in the history
  • Loading branch information
faithandbrave committed Dec 27, 2022
1 parent 192dae6 commit e68299b
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lang/cpp17/inline_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ function-specifier:
explicit
```

## 備考
- 名前のないクラスのオブジェクトは、インライン指定しても同じ実体にはならない

This comment has been minimized.

Copy link
@k-satoda

k-satoda Dec 28, 2022

Contributor

@faithandbrave (@yohhoy) リンク先のコード見ましたが、この場合は
extern struct {} x; を2つ並べて書いた場合と同様の型の不一致(同じ字面でも
別々の型による宣言)がコンパイル単位を跨いで発生している状態となって、
エラーにならなかった場合の動作は未定義となりそうです。
https://timsong-cpp.github.io/cppwp/n4861/basic.link#11

... the types specified by all declarations referring to a given variable or
function shall be identical, ... A violation of this rule on type identity
does not require a diagnostic.

This comment has been minimized.

Copy link
@faithandbrave

faithandbrave Dec 28, 2022

Author Member

@k-satoda @yohhoy
こんな感じでどうでしょうか。

「無名クラスのオブジェクトをインライン指定した場合、それらの型は翻訳単位ごとに異なることになる。そのため、型の不一致によってプログラムは不適格になるか、もしくは未定義動作となる (実装によって異なる実体として扱われる場合がある)」

This comment has been minimized.

Copy link
@k-satoda

k-satoda Dec 28, 2022

Contributor

@faithandbrave エラーが出る場合を「不適格」それ以外を「未定義」とされていそうですが、
エラーが出るのも未定義の範囲内の(望ましい)動作の一つなので、単に未定義でよいかと。
その点を除けば誤解を与える記述ではなくなっていると思います。

・・・改めて記事を見直すと、すでにこの場合について述べた箇所があることに気付きました。

翻訳単位によって異なる型、初期値で変数を定義すると、プログラムは正常に動作しない可能性がある (不適格(例外付き診断不要))。

ここでは「不適格(例外付き診断不要)」とされています。ここらへんの分類を改めて考えないで済ませる
対応として、ここに「無名クラスのオブジェクトをインライン指定した場合、それらの型は
翻訳単位ごとに異なることになり、これに該当する。この場合の動作例として、
エラーも出ず異なる実体として扱われることがあった。」などと加えるといいかなーと思いました。

This comment has been minimized.

Copy link
@faithandbrave

faithandbrave Jan 3, 2023

Author Member

@k-satoda
再度の確認・ご指摘ありがとうございます。
備考での言及をやめて、仕様の例として無名クラスオブジェクトの説明をするよう修正しました。
651153b



##

```cpp example
Expand Down Expand Up @@ -264,3 +268,4 @@ func
- [P0386R2 - Inline Variables](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0386r2.pdf), 2016-06-24
- [P0607R0 - Inline Variables for the Standard Library](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0607r0.html), 2017-02-27
- [C++1z インライン変数 - Faith and Brave - C++で遊ぼう](https://faithandbrave.hateblo.jp/entry/2016/11/11/172954)
- [インライン変数が翻訳単位毎に別々の実体となってしまう - Stackoverflow](https://ja.stackoverflow.com/questions/93046/)

0 comments on commit e68299b

Please sign in to comment.