Monday 15 June 2015

c++ - Enable function depending on template class variables -


I have a simple matrix class that looks like this:

  template & lt ; Int m, int n & gt; Class Matrix {Public: std :: enable_if & lt; Std :: is_same & lt; Matrix, matrix • 4,4 & gt; & Gt; Value, Zero & gt; Translate (float x, float y, float z) {// do stews} private: float mat [m * n]; };   

I hope that the std :: enable_if function will only be available if I have a matrix and lt; 4,4> gt; It appears that this is wrong.

  int main () {matrix <4, 3> Mat3; Mat3.translate (1.0f, 1.0f, 1.0f); Return 0; }   

The above code is not compiled with any errors What am I doing?

I know that I can put just a static_assert (m == 4 & amp; n == 4) in the function body, but I'm cleaner I am looking for solution and want to learn something about templates in this process.

For the starter, you should:

  Typename std :: enable_if & lt; Std :: is_same & lt; Matrix, matrix <4,4> gt; & Gt; :: value, zero & gt; ::   

Instead of:

  std :: enable_if  gt; & Gt; :: value, zero & gt;   

But this will still not work, because this category will be evaluated when the class template is instant, thus an error occurs, even if you ever translate () .

If you can use C ++ 11, you can do the following:

  template & lt; Typename t = matrix & gt; Typename std :: enable_if & lt; Std :: is_same & lt; T, matrix & lt; 4,4 & gt; :: Price & gt; :: Type translate (float x, float y, float z) {// do stof}   

here is one. Notice how the compilation error has started by calling translation () from the mere rebound of the notice, matrix <4,3> . .

No comments:

Post a Comment