C++ C++;助推变量问题
我知道,C++ C++;助推变量问题,c++,boost,variant,compile-time,boost-mpl,C++,Boost,Variant,Compile Time,Boost Mpl,我知道,boost::variant在其背后使用了boost::mpl的东西,并且有一个与mpl兼容的typedeftypes 假设我有一个简单的typedef:typedef boost::variant 现在我有了另一个模板函数,比如: template <typename T> T function() { // ... } 我唯一不知道的是这个条件 现在-如果T是Variant::types的一部分,我确实认为可以进行编译时查询 有人知道怎么做吗?确实有可能,Vari
boost::variant
在其背后使用了boost::mpl
的东西,并且有一个与mpl兼容的typedeftypes
假设我有一个简单的typedef:typedef boost::variant代码>
现在我有了另一个模板函数,比如:
template <typename T> T function() {
// ...
}
我唯一不知道的是这个条件
现在-如果T
是Variant::types
的一部分,我确实认为可以进行编译时查询
有人知道怎么做吗?确实有可能,Variant::types
满足Mpl.序列类型的要求,因此可以像任何序列一样进行查询
因此,使用boost::mpl::contains
来自:
//使用C++0x语法演示应该用什么条件替换
模板
使用条件=boost::mpl::contains
没有比这更简单的了,只要你知道;)
如果您还需要一些算法,MPL手册全文的格式是可用的。+1。由于问题是用C++标记的,而不是C++ 0x,所以您可能想使用语法添加关于<代码>的注释。另外,您不是想在
boost::mpl::contains
中编写Variant::types
而不是Variant
?
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
}
// using C++0x syntax to demonstrate what CONDITION should be replaced with
template <typename T>
using Condition = boost::mpl::contains<Variant::types,T>