作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的任务中,我想创建一个模板,它只接受在下面的类 CDataFormat 中定义的 typedef:
class CDataFormat{
public:
typedef unsigned short element_t;
typedef unsigned int accumulation_t;
typedef double division_t;
};
现在下面的实现工作正常。
template<typename DF, int SIZE>
class CFilter{
private:
DF m_memberName[SIZE];
public:
void whatever(){
//CFilter<CDataFormat::division_t, 8> smth; // Just a small test
}
};
但是,不能确保模板只接受 CDataFormat 的成员。
我该怎么做?
最佳答案
您可以使用 static_assert
来报告误用:
template <typename DF, int SIZE>
class CFilter{
static_assert(std::is_same<CDataFormat::element_t, DF>::value
|| std::is_same<CDataFormat::accumulation_t, DF>::value
|| std::is_same<CDataFormat::division_t, DF>::value, "Unexpected type");
private:
DF m_memberName[SIZE];
};
关于c++ - 如何将类的某些 typedef 传递给模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428905/
我是一名优秀的程序员,十分优秀!