tree.h中代码重复
template<typename Functor, char Operator>
class binary_operation : public node
{
// ... unimportant details ...
unsigned evaluate() const;
void print(std::ostream& os) const;
};
typedef binary_operation<std::plus<unsigned>, '+'> addition;
typedef binary_operation<std::multiplies<unsigned>, '*'> multiplication;
// ...
tree.cpp
template<typename Functor, char Operator>
unsigned binary_operation<Functor, Operator>::evaluate() const
{
// ... unimportant details ...
}
template<typename Functor, char Operator>
void binary_operation<Functor, Operator>::print(std::ostream& os) const
{
// ... unimportant details ...
}
template class binary_operation<std::plus<unsigned>, '+'>;
template class binary_operation<std::multiplies<unsigned>, '*'>;
// ...
正如你所看到的,有在头文件和明确的类型定义之间的一些代码重复实现文件中的类模板实例化。有没有办法摆脱不需要像往常一样将“一切”放在头文件中的重复?
我认为你不能在.cpp文件中写'template class addition;',这是一种耻辱。 – 2012-07-10 13:54:11
nope :('错误:在'class''后面使用typedef-name'addition' – fredoverflow 2012-07-10 13:55:41
我在assuning decltype也无济于事......但C++仍然有旧的预处理器......你可以用宏通用部分:-) – 2012-07-10 17:38:56