How to use 'ref' at compile time?

struct Matrix(int row, int col){ /* ... */ }

// finds the inverse using Gauss–Jordan elimination
pure M inverse(M)(const ref M m){ /* ... */ }

The reason mis refdue to performance. Obviously, I don't want large matrices to be copied every time the opposite is required, and so far this has worked fine.

But this has become a problem in situations where the opposite is required at compile time:

mixin template A(){

  alias Matrix!(3, 3) Matrix3x3;

  static Matrix3x3 computeSomeMatrix(){ }

  immutable Matrix3x3 _m = computeSomeMatrix();
  immutable Matrix3x3 _m_1 = inverse(computeSomeMatrix());  // error
}

To fix the error, I need to change mto non-ref, but this means that the matrices will be copied every time it is called inverse(). What should I do?

+5
source share
1 answer

I see one of two options. First, create a version that takes the value r. This is often annoying when a function does not work with rvalues ​​anyway. You need a simple wrapper:

pure M inverse(M)(const ref M m){ /* ... */ }
pure M inverse(M)(const M m){ inverse(m); }

, , .

auto ref. , .

pure M inverse(M)(const auto ref M m){ /* ... */ }

ref, , ref, , .

+4

All Articles