Boost/C++ eval_if fails
Boost/C++ eval_if fails
我遇到了一个非常困难的问题,下面的程序说明了这个问题:看起来第51行中的eval_if不能正常工作。也就是说,当这两种类型都不是安全类型时,调用元函数get_promotion_policy。我已经研究了两天了,还是看不出发生了什么。任何来自TMP天才的帮助将是非常感激的。
#include <type_traits>
#include <boost/mpl/and.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
struct safe_tag {};
template<class T, class P = void>
struct safe : public safe_tag {
T m_t;
typedef P PromotionPolicy;
};
template<class T>
struct is_safe : public
//std::is_arithmetic<T>
std::is_base_of<safe_tag, T>
{};
template<class T, class U>
struct common_policies {
static_assert(
boost::mpl::or_<
is_safe<T>,
is_safe<U>
>::value,
"at least one type must be a safe type"
);
template<typename Z>
struct get_promotion_policy {
static_assert(
is_safe<Z>::value,
"only safe types have promotion policies"
);
typedef typename Z::PromotionPolicy type;
};
// if both types are safe, the policies have to be the same!
static_assert(
boost::mpl::eval_if<
boost::mpl::and_<
is_safe<T>,
is_safe<U>
>,
typename std::is_same<
typename get_promotion_policy<T>::type,
typename get_promotion_policy<U>::type
>::type,
boost::mpl::identity<boost::mpl::true_>
>::type::value,
"if both types are safe, the policies have to be the same!"
);
// now we've verified that there is no conflict between policies
// return the one from the first safe type
typedef typename boost::mpl::if_<
is_safe<T>,
T,
typename boost::mpl::if_<
is_safe<U>,
U,
//
boost::mpl::void_
>::type
>::type safe_type;
typedef typename get_promotion_policy<safe_type>::type promotion_policy;
};
common_policies<int, safe<int> > t;
int main(){
return 0;
}
typename std::is_same<
typename get_promotion_policy<T>::type,
typename get_promotion_policy<U>::type
>::type
触发get_promotion_policy<T>
和get_promotion_policy<U>
的实例化,即使它在假分支中。您必须报告评估结果,例如:
// if both types are safe, the policies have to be the same!
static_assert(
boost::mpl::eval_if<
boost::mpl::and_<
is_safe<T>,
is_safe<U>
>,
std::is_same<
typename boost::mpl::eval_if<is_safe<T>, get_promotion_policy<T>, boost::mpl::identity<void>>::type,
typename boost::mpl::eval_if<is_safe<U>, get_promotion_policy<U>, boost::mpl::identity<void>>::type
>,
boost::mpl::identity<boost::mpl::true_>
>::type::value,
"if both types are safe, the policies have to be the same!"
);
演示相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- 为什么简单的算术减法在"if"条件下不起作用?
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句