【发布时间】:2014-04-04 14:07:26
【问题描述】:
我四处寻找一个好的解决方案来避免模板类的每个特殊化的代码重复。
这是一个示例代码:
template<class T>
class C
{
int foo();
}
现在是默认值的定义:
template<class T>
C<T>::foo() { return 0; }
现在特殊模板的专业化
template<> C<int>::foo() { ... do a lot of stuff and return n .... }
template<> C<double>::foo() { ... do a lot of stuff and return n .... }
template<> C<int>::foo() { ... do a lot of stuff and return n .... }
现在我必须为专业化复制代码。但总的来说,它是相同的代码。
我的问题是: 在这里避免代码重复的最佳解决方案是什么?如何隐藏实现?也许通过使用 noname-namespace 或 impl-namespace ?
亲切的问候, 彼得
【问题讨论】:
标签: c++ templates code-duplication