c++pointer-aliasingrestrict-qualifier

Prevent two object internals from aliasing


I have a function signature similiar to this

void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out);

Internally the matrix class has a float* data; that represents the m x n components. I'd like to tell the compiler that a and b do not alias the out matrix so it doesn't do a ton of load-stores.

How would I go about doing that? I know I could pass in pointers to the function signature and mark the pointers with __restrict(in MSVC) but I'd like to keep the idiom of object passed by reference where the object contains pointers to memory.

I also know that __restrict does not work on object references.


Solution

  • Write a non-exported (file-static, private) multiplication function that takesfloat* arguments, mark the arguments with restrict. Make Multiply call this function.