my code breaks unique pointer rule?

  • Thread starter Thread starter George3
  • Start date Start date
G

George3

Hello everyone,


Unique pointer should not allow aliasing. But my code which contains
unique pointer aliasing and also compile/run ok. Anything wrong?


Code:
--------------------

__interface IFoo {

public:

virtual int foo ([unique] char *ptr1, [unique] char *ptr2) = 0;
};

class Foo : public IFoo {

public:

int foo (char *ptr1, char *ptr2)
{
char array1[] = "Hello";

ptr1 = array1;
ptr2 = array1; // should be wrong, no aliasing allowed, but compile and run ok

return 0;
}
};


int main()
{
char* ptr1 = 0;
char* ptr2 = 0;

Foo f;
f.foo(ptr1, ptr2);

return 0;
}

--------------------



thanks in advance,
George
 
George3 said:
Hello everyone,


Unique pointer should not allow aliasing. But my code which contains

Wrong. The server isn't required to detect and reject aliasing. The client
is required to never use aliasing, or else suffer undefined behavior. Since
you've broken the interface contract, the server can do whatever -- succeed,
fail loudly, fail silently, etc.
unique pointer aliasing and also compile/run ok. Anything wrong?

It only ran ok because you got lucky. Don't rely on things working when you
break the rules.
 
Back
Top