In the subsection 'Simple Assignment' of spec, it has the constraint
It seems that the following code snippet violates this but clang -Weverything
doesn't give any warnings. What am I interpreting wrong?
const int i = 5;
int j = i;
Integer assignment is done by value. const int
means that the 4 (or 8) bytes of that int
are read-only. Assignment just copies the bytes of i
into another location specified by j
. This involves reading i
and writing to j
, both of which are declared as valid operations.
Your code doesn't perform any assignment, so it does not violate any rules about assignment.
(Your code is a declaration with initialization.)
try i=j; you will see. gcc give you this: "error: assignment of read-only variable"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.