Microsoft thinks this a good idea?

closed account (1yR4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
const int x = 10;
const int* foo = &x;

void f(const int*& ptr) {
  ptr = foo;
}

int main() {
  int* test = 0;
  f(test);
  *test = 9001;
  return 0;
}

Compiles with the /Ze switch...but my question is...why?
Last edited on
Compiler: Visual C++
/Ze Microsoft Language Extensions

And is not a bug, but a feature!
Passing a Non-Const Pointer Parameter to a Function that Expects a Reference to a Const Pointer Parameter
http://msdn.microsoft.com/en-us/library/34h23df8%28v=vs.71%29.aspx
darkestfright wrote:
Compiles with the /Ze switch...but my question is...why?
My question would be: why not without. Adding const to an objet should always be possible. Some compiler have problems when there's more than 1 '*' in a row. But since nothing dangerous can happen it should be allowed.
Adding const to an objet should always be possible.
But the result may not be what you expect.
test is not const
foo can not assign to test
@coder777: the problem is that x it is supposed to be constant, but you can change its value with test.
1
2
3
4
const int *A; //points to read-only memory
int *B; // read-write memory
A = B; // fine, we are loosing privileges
B = A; // illegal 
Last edited on
yep, i should read it more carefully. that way you can modify a const without even beeing aware of not to mention cast it. certainly no good
Compiler: Visual C++
/Ze Microsoft Language Extensions
And is not a bug, but a feature!
Passing a Non-Const Pointer Parameter to a Function that Expects a Reference to a Const Pointer Parameter
http://msdn.microsoft.com/en-us/library/34h23df8%28v=vs.71%29.aspx


Wow, what a bizarre feature! Modifying a const reference to something within a function just seems like a bad idea.
Wow, what a bizarre feature!
Typically such hacks are put in to avoid recoding large swathes of code. This gives the products time to catch up.

An example of this was the scope of variables in for loops. Microsoft was late in doing this correctly and required a command line option to switch on support for the feature.
closed account (1yR4jE8b)
That reminds of one time I disabled all the C++ language extensions in Visual Studio and I started getting compiler errors pointing to standard headers... durp a dee
Topic archived. No new replies allowed.