use*_*536 2 c++ template-specialization
我上课了
template <typename T>
class C
{
static const int K=1;
static ostream& print(ostream& os, const T& t) { return os << t;}
};
Run Code Online (Sandbox Code Playgroud)
我想将C专门化为int.
//specialization for int
template <>
C<int>{
static const int K=2;
}
Run Code Online (Sandbox Code Playgroud)
我想要保留int的默认打印方法,只需更改常量.对于某些特化,我想保持K = 1并更改print方法,因为没有<<运算符.
我该怎么做呢?
你可以这样做:
template <typename T>
class C {
static const int K;
static ostream& print(ostream& os, const T& t) { return os << t;}
};
// general case
template <typename T>
const int C<T>::K = 1;
// specialization
template <>
const int C<int>::K = 2;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
537 次 |
| 最近记录: |