【发布时间】:2015-02-19 19:30:13
【问题描述】:
我正在尝试确定是否有办法在编译时同时对两种不同类型进行条件检查。
例子:
template <typename T>
class MyClass
{
returnVal myFunction() const
{
// Is there a compile time way of doing this? std::conditional?
return myConditionFunction(std::is_same<char, T> || std::is_same<unsigned char, T>);
}
returnVal myConditionFunction(std::true_type& const) const
{
// perform calculations on char or unsigned char
}
returnVal myConditionFunction(std::false_type& const) const
{
// perform calculations on non-char/unsigned char types
}
};
如果类型是 char 或 unsigned char,我想调用一个函数。
编辑:更新代码以显示我正在使用模板类。
【问题讨论】:
-
你目前所做的已经是编译时的事情了。
-
但是 (std::is_same
|| std::is_same ) 不起作用。我可以做一个或另一个,但不能同时做。 -
你擅长的 char 和 unsigned char 的模板怎么样?
标签: c++ c++11 stl compile-time