- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The Intel Compiler allows assigning an int* to a const int* &, which could cause unexpected behavior as in the following example:
[cpp]int* p; const int* & r = p; // assign to reference to a pointer to const int - this must not be allowed const int immutable = 0; r = &immutable; // assign p to address of immutable through r *p = 1; // whoops, now 0 = 1![/cpp]
The code above compiles without errors,although the ASM generated ignores the assignment.
This potential issue may be hidden in template code, so it would be hard to detect without a compile error.
Regards,
Ricardo.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team for fix.
Thank you.
-Yang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
tt.cpp(8): error: a reference of type "const int *&" (not const-qualified) cannot be initialized with a value of type "int *"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team for
[SergeyK] C++ language specificationshave tobe checked.
fix.
Thank you.
-Yang
Here are some results for different C/C++ compilers:
MS C/C++ with VS2005: Error C2440: 'initializing' : cannot convert from 'int *' to 'const int *&'
MinGW C/C++ v3.4.2 : Error: invalid initialization of reference of type 'const int*&' from expression of type 'int*'
Borland C/C++ v5.5.1 : Successfully compiled
Turbo C/C++ v3.0.0 : Successfully compiled
It looks like specifications have changed because two legacy C/C++ compilers compiled the code.
The subjectalso raises a veryold question: Could a value of some variable declared as 'const'be modified later?
Best regards,
Sergey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thanks for reporting this issue. It should be a compiler bug. I have reported it to the compiler team
[SergeyK]After a series of tests with different C/C++ compilersIconfirm it.
for fix.
Thank you.
-Yang
Please take a look at a screenshot:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The subjectalso raises a veryold question: Could a value of some variable declared as 'const'be
modified later?
...
Here is a Test-Case:
...
const int iN = 0;
printf( "Initial value for Variable of type < const int > is: %ld\n", iN );
for( int i = 0; i < 4; i++ )
{
printf( "New value for Variable of type < const int > is: %ld\n",
++( const_cast< int & >( iN ) ) );
}
printf( "Final value for Variable of type < const int > is: %ld\n", const_cast( int & )( iN ) );
printf( "Final value for Variable of type < int > is: %ld\n", iN );
...
Output:
Initial value for Variable of type < const int > is: 0
New value for Variable of type < const int >is: 1
New value for Variable of type < const int >is: 2
New value for Variable of type < const int >is: 3
New value for Variable of type < const int >is: 4
Final value for Variable of type < const int > is: 4
Final value for Variable of type < int > is: 0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Best regards,
Sergey
The constness is supposed to be a compile-time helper against programming errors, but the programmer still has the ultimate decision about how the variable shall be used.When const_cast is used, the programmer is explicitly telling the compiler "I know what I'm doing, don't complain", so it's allowed to modify a const variable in that case.
However the error that I reported initially is not like an explicit const cast, instead it may be hidden by template transformations that end up generating incorrect/invalid code against the programmer's will, thus the importance of detecting that situation with a compile error.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page