Can __restrict__ apply to shared_ptr <T>?

Smart pointers are pointers below, so is there a way to define the shared_ptr parameter for a function as not smoothing another shared_ptr or another pointer of any type?

Or is it somehow unnecessary?

I'm interested in the gcc> = 4.2 and llvm-clang> = 2.0 compilers (the answers for other compilers will also be interesting).

+8
c ++ g ++ clang shared-ptr restrict-qualifier
source share
2 answers

Just highlight the pointers with .get() and mark them as __restrict__ . Remember that including __restrict__ in function parameters is the same as putting __restrict__ in local variables. In particular, the compiler does not try to stop a function call with two pointers, which obviously point to the same object; e.g. foo(i,i) .

If you want to promise the compiler that some pointers do not refer to each other, allowing the compiler to do more optimizations, use this code below and do your operations with xp and yp instead of x and y .

 #include<iostream> #include<memory> using namespace std; void foo(shared_ptr<int> x, shared_ptr<int> y) { int * __restrict__ xp = x.get(); int * __restrict__ yp = y.get(); } int main() { shared_ptr<int> i = make_shared<int>(3); shared_ptr<int> j = make_sharet<int>(4); foo(i,j); } 
+7
source share

If you want to perform non-smooth operations on a base object associated with a shared pointer, you can explicitly delegate a working routine that takes a parameter with an unaliasized pointer:

 void worker (mytype *__restrict x, mytype *__restrict y) { // do something with x, y with a no-alias guarantee } int main() { std::shared_ptr<mytype> p(new mytype); std::shared_ptr<mytype> q(new mytype); // explicitly delegate the shared object worker(p.get(), q.get()); return 0; } 

I'm not sure what you mean, but it will allow the safe management of high-level memory with a smart pointer, while working at a low level is probably more efficient with pointers without an alias.

As @BenVoigt noted, restrict is only the official part of c99 - c++ shouldn't know anything about it. MSVC supports it anyway through __restrict , and, as you said, GCC has __restrict__ .

Hope this helps.

+5
source share

All Articles