jeudi 25 août 2016

How to conditionally add a function to a class template?

I have a Matrix class template as follows:

template<typename T, std::size_t nrows, std::size_t ncols>
class Matrix
{
    T data[nrows][ncols];
public:
    T& operator ()(std::size_t i, std::size_t j)
    {
        return data[i][j];
    }
};

What I want is to define a .setIdentity() function only for instantiations when nrows==ncols is true at compile time. And there will be no definition of .setIdentity() when nrows==ncols is false.

What I am trying is using enable_if idiom, but that will define the function for all cases. Isn't it?

Aucun commentaire:

Enregistrer un commentaire