这完全取决于您想在无效代码上引发什么样的错误/失败。这是一种可能性(撇开明显的static_assert(Width==Height, "not square matrix");
)
(C++98 风格)
#include<type_traits>
template<int Width, int Height, typename T>
class Matrix{
public:
template<int WDummy = Width, int HDummy = Height>
static typename std::enable_if<WDummy == HDummy, Matrix>::type
Identity(){
Matrix ret;
for (int y = 0; y < Width; y++){
// elements[y][y] = T(1);
}
return ret;
}
};
int main(){
Matrix<5,5,double> m55;
Matrix<4,5,double> m45; // ok
Matrix<5,5, double> id55 = Matrix<5,5, double>::Identity(); // ok
// Matrix<4,5, double> id45 = Matrix<4,5, double>::Identity(); // compilation error!
// and nice error: "no matching function for call to ‘Matrix<4, 5, double>::Identity()"
}
编辑:在 C++11 中,代码可以更紧凑和清晰,(它可以在clang 3.2
但不是gcc 4.7.1
,所以我不确定它有多标准):
(C++11 风格)
template<int Width, int Height, typename T>
class Matrix{
public:
template<typename = typename std::enable_if<Width == Height>::type>
static Matrix
Identity(){
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};
2020 年编辑:(C++14)
template<int Width, int Height, typename T>
class Matrix{
public:
template<typename = std::enable_if_t<Width == Height>>
static Matrix
Identity()
{
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};
(C++20) https://godbolt.org/z/cs1MWj
template<int Width, int Height, typename T>
class Matrix{
public:
static Matrix
Identity()
requires(Width == Height)
{
Matrix ret;
for(int y = 0; y < Width; y++){
// ret.elements[y][y] = T(1);
}
return ret;
}
};