I have a Matrix class template as follows:
template
class Matrix
{
T data[nrows][ncols];
public:
A basic, but simple solution not mentioned by any other answer: you can use std::conditional and inheritance.
It follows a minimal, working example:
#include
#include
struct HasSetIdentity {
void setIdentity() { }
};
struct HasNotSetIdentity {};
template
class Matrix: public std::conditional<(nrows==ncols), HasSetIdentity, HasNotSetIdentity>::type
{
T data[nrows][ncols];
public:
T& operator ()(std::size_t i, std::size_t j)
{
return data[i][j];
}
};
int main() {
Matrix m1;
m1.setIdentity();
Matrix m2;
// Method not available
// m2.setIdentity();
}
You can still move data down the hierarchy if you need them to be shared by all the subobjects.
It mostly depends on the real problem.