我专注于数据类型的'less'(谓词).
代码如下所示:
template<>
struct std::less<DateTimeKey>
{
bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
{
// Some code ...
}
}
Run Code Online (Sandbox Code Playgroud)
编译时(在Ubuntu 9.10上使用g ++ 4.4.1),我得到错误:
不同命名空间中'template struct std :: less'的专业化
我做了一些研究,发现有一个'解决方法'涉及将特化包装在std命名空间中 - 即将代码更改为:
namespace std {
template<>
struct less<DateTimeKey>
{
bool operator()(const DateTimeKey& k1, const DateTimeKey& k2) const
{
// Some code ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
确实,关闭了编译器.然而,这个解决方案来自一个5岁的职位(由'伟大的'Victor Bazarof而不是[双关语]).这个解决方案还有很长的路要走,还是有更好的方法来解决这个问题,还是"老方法"仍然有效?
我想对模板类使用部分特化,以便该模板类的所有子节点都将使用该特化.让我用一个例子来解释:)
template < typename T, unsigned int rows, unsigned int cols>
class BaseMatrix {...};
Run Code Online (Sandbox Code Playgroud)
这个类将有子项指定矩阵的结构,如稀疏,密集,对角线,..
template < typename T, unsigned int rows, unsigned int cols>
class DiagonalMatrix : public BaseMatrix<T,rows,cols>{..}
Run Code Online (Sandbox Code Playgroud)
然后这些类将再次生成指定存储的子节点:堆栈数组,向量,列表,队列,..
template < typename T, unsigned int rows, unsigned int cols>
class StackDiagonalMatrix : public DiagonalMatrix<T, rows, cols> {..}
Run Code Online (Sandbox Code Playgroud)
然后有一个类Matrix,它提供所有数学功能.这个模板类实现了operator +,operator-等......
template <typename T,
template<typename, unsigned, unsigned> class MatrixContainer,
unsigned Rows,
unsigned Cols>
class matrix;
Run Code Online (Sandbox Code Playgroud)
对于这最后一堂课,我想写下这样的专业:
template <typename T,unsigned Rows, unsigned Cols>
class matrix<T, BaseMatrix, Rows, Cols> {};
template <typename …Run Code Online (Sandbox Code Playgroud)