提升::enable_if有两个条件
boost::enable_if with two conditions
正如你在下面的示例中看到的,我目前使用boost::enable_if
作为分配函数的返回值。目标是避免抽象类型的编译错误:
template <typename T>
typename boost::enable_if<boost::is_abstract<T>,T*>::type no_abstract_new()
{
assert(false);
return 0;
}
template <typename T>
typename boost::disable_if<boost::is_abstract<T>,T*>::type no_abstract_new()
{
return new T;
}
现在,我还想排除从自己的名为has_no_default_constructor
的类继承的类。有没有办法在boost::enable_if
的情况下or
?像这样不正确的代码:
template <typename T>
typename boost::enable_if<boost::is_abstract<T>
|| boost::is_base_of<has_no_default_constructor,T>,T*>::type default_constructor_new()
{
assert(false);
return 0;
}
template <typename T>
typename boost::disable_if<boost::is_abstract<T>
|| boost::is_base_of<has_no_default_constructor,T>,T*>::type default_constructor_new()
{
return new T;
}
还是我必须实现自己的特质类来完成这项工作?(我对此完全迷茫了。我理解这个想法,但我觉得自己可以做(
笔记:
- 出于兼容性原因,我不使用 C++11
- 我知道
has_default_constructor
存在于 C++11 中,但不是在 C++11 之前 boost::has_default_constructor
存在,但如果编译时没有 C++11,则只是boost::has_trivial_constructor
的别名
还有
template <bool B, class T = void> struct enable_if_c;
请注意,它采用bool
作为第一个参数而不是类型。因此,以下内容应该没问题
template <typename T>
typename boost::enable_if_c<boost::is_abstract<T>::value
|| boost::is_base_of<has_no_default_constructor,T>::value
,T*>::type default_constructor_new()
{
assert(false);
return 0;
}
对于其他重载也是如此。
相关文章:
- 如何在for循环中包含两个索引值的测试条件
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 提升::enable_if有两个条件
- 最佳无分支有条件选择两个SSE2填充双打
- 什么保证两个不相关的线程中的不同不相关对象没有(不可避免的)争用条件?
- 使用 if 语句满足两个条件
- 按两个条件对lambda表达C 进行排序
- 使用两个条件对对象的向量进行排序
- 如何在没有 c++ 中的数组/算法的情况下对两个条件的字符串进行排序
- 使用两个条件迭代自定义对象的向量
- 如何有条件地为 Eclipse 中的交叉编译项目包含两个不同名称的相同版本的库
- 使用两个条件对结构数组进行排序 C++
- 在do-while循环中同时检查两个条件
- 两个条件变量和避免死锁
- 逗号(,)在条件部分中两个表达式之间的for循环中的工作方式
- 如何通过两个值在向量中的位置进行条件比较
- C++比较两个字符来验证循环条件
- if条件下两个字符串比较的优化代码
- 如何在Linux上同时等待两个条件
- 我需要在两个条件下实现while循环