cdecl "Const pointer to T" versus C++ const_iterator

OPEN

Consider cdecl's rather simple: ( https://cdecl.org/?q=int+*+const+ptr )

int * const ptr
declare ptr as const pointer to int

From my understanding, in C's terminology, a "const pointer", ptr, cannot be incremented or decremented. Fair enough. The meaning and interpretation of the term is common-sensical and fits in with a user's (at least my) mental model.

Thus, the following would not compile in C++ or in C.

void increment_const_pointer(int * const ptr, int size){
    for(int i = 0; i < size; i++){
        *ptr = 42; // okay! The thing pointed to can mutate
        ++ptr; //Not OK as the pointer is a const pointer!
    }
}

C++'s terminology and semantics of const_iterator, seems at odds with the above well-understood common-sense meaning of "const pointer", as a legacy from C.

For e.g., the following is fine in C++

void increment_const_iterator(){
    std::vector<int> vecint = {1,2,3};
    typedef std::vector<int>::const_iterator VCI;
    for(VCI iter = vecint.begin(); iter != vecint.end(); ++iter)
        printf("%d\n", *iter);
}

Godbolt link of above code: https://godbolt.org/z/e7qaWed4a

Is there a reason for the allowing a const_iterator to be incremented while a "const pointer" cannot be? Reason why I ask is that in many places such as here and on SO, one is asked, heuristically at least, to "think of an iterator as a pointer", or is told that "the compiler will implement an iterator just as a pointer", etc., even though these heuristics may not be completely accurate.

Posted Dec 10, 2025, 3:02 AM UTC

10
Loading comments…