5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

const と auto と pointer

Last updated at Posted at 2021-05-14

よくわかんなくなるので。

宣言 実際の t の型 値が不変 ポインタが不変
const auto t = new T(); T *const ×
const auto *t = new T(); const T* ×
const auto *const t = new T(); const T *const
auto const t = new T(); T *const ×
auto const *t = new T(); const T* ×
auto *const t = new T(); T *const ×
auto const *const t = new T(); const T *const

覚え方としては

  1. const auto が auto const に置き換えられる
  2. auto が T* ないし T に置き換えられる
  3. T const が const T に置き換えられる

で実際の型が決まったうえで、

  • const のすぐ左にくるものが不変となる
  • const が一番左のときはすぐ右にくるものが不変となる

で値やポインタが不変かどうかが分かる。

ようするに const auto って書かないで auto const って書くようにしてたほうがすんなり理解できるようになるんじゃないかな、たぶん

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?