Last active
February 27, 2019 13:20
-
-
Save nixiz/d4478541ce7702f04fc245a246de261d to your computer and use it in GitHub Desktop.
const placement matters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
void test(T p) { | |
(*p)++; // increment by value | |
p++; // increment pointer to next element | |
} | |
void testrun1() | |
{ | |
int value[2] {0, 1}; | |
test<int *>(&value[0]); // 1. compiles and runs successfuly | |
test<int const*>(&value[0]); // 2. error on line: (*p)++; | |
test<const int*>(&value[0]); // 3. error on line: (*p)++; | |
test<int *const>(&value[0]); // 4. error on line: p++; | |
test<int const*const>(&value[0]); // 5. error on lines: (*p)++ and p++; | |
} | |
void testrun2() | |
{ | |
int * iptr = nullptr; | |
(*iptr)++; // 1. will compile successfuly | |
iptr++; // 1. will compile successfuly | |
int const * icptr = nullptr; | |
(*icptr)++; // 2. error on line: (*icptr)++; | |
icptr++; // 2. will compile successfuly | |
const int * ciptr = nullptr; | |
(*ciptr)++; // 3. Same as 2. | |
ciptr++; // 3. Same as 2. | |
int * const iptrc = nullptr; | |
(*iptrc)++; // 4. will compile successfuly | |
iptrc++; // 4. error on line: iptrc++; | |
int const * const icptrc = nullptr; | |
(*icptrc)++; // 5. error on line: (*icptrc)++ | |
icptrc++; // 5. error on line: icptrc++ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment