Smart pointers are pointers underneath, so is there any way of defining a shared_ptr
parameter to a function as not aliasing another shared_ptr
, or another pointer of any sort?
Or is this, for some reason, unnecessary?
I'm concerned with the gcc >=4.2 and llvm-clang >=2.0 compilers (answers for other compilers would also be interesting).
Just extract the pointers with
.get()
and mark them as__restrict__
. Remember, putting__restrict__
into the function parameters is the same as putting__restrict__
on local variables. In particular, the compiler doesn't attempt to stop you from calling the function with two pointers that obviously point to the same object; e.g.foo(i,i)
.If you want to make a promise to the compiler that certain pointers don't reference each other, allowing the compiler to do more optimizations, then use this code below and do your operations through
xp
andyp
instead ofx
andy
.