繁体   English   中英

C ++ Boost变体问题

[英]C++ boost variant question

我知道boost::variant在其后面使用boost::mpl东西,并且具有mpl兼容的typedef types

假设我有一个简单的typedef: typedef boost::variant<bool, int> Variant;

现在,我还有另一个模板函数,让我们说:

template <typename T> T function() {
   // ...
}

我希望此函数在两种情况下具有不同的作用:当TVariant::types的一部分时,而当不是。

显然,我必须做类似的事情

template <typename T>
typename boost::enable_if<CONDITION, T>::type function() {
   // Implementation for the case T is in Variant::types
}

template <typename T>
typename boost::disable_if<CONDITION, T>::type function() {
   // Implementation for the case T is ***NOT*** in Variant::types
}

我唯一不知道的就是这个CONDITION

现在-我确实认为如果TVariant::types的一部分,则可以进行编译时查询。

有人知道吗?

实际上, Variant::types可能满足Mpl.Sequence类型的要求,因此可以像任何序列一样查询。

因此,从此处使用boost::mpl::contains

// using C++0x syntax to demonstrate what CONDITION should be replaced with
template <typename T>
using Condition = boost::mpl::contains<Variant::types,T>

当您了解它时,没有比这更简单了;)

如果您需要更多算法,则可以使用HTML格式的完整MPL手册。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM