是否有'choose< bool,typename,typename>'的标准构造? [英] Is there a standard construct for 'choose<bool,typename,typename>'?

查看:82
本文介绍了是否有'choose< bool,typename,typename>'的标准构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我需要满足以下条件:

I sometimes find myself in need for the following:

template<bool B, typename T1, typename T2>
struct choose{
  typedef T1 type;
};

template<typename T1, typename T2>
struct choose<false, T1, T2>{
  typedef T2 type;
};

我用它有条件地选择一种或另一种.现在,标准库中已经有可以完全做到这一点的东西吗? Boost.MPL具有类似的内容,但这并不完全相同(采用某种类型,而不是布尔),我不想在这件事上包含Boost. :)

I use this to conditionally choose one type or the other. Now, is there already something in the standard library that does exactly this? Boost.MPL has something similar, but that isn't exactly the same (takes a type, not a bool) and I don't want to include Boost for this little thing. :)

推荐答案

是的:它在C ++ 0x中称为std::conditional(在Boost中称为boost::conditional).

Yes: it is called std::conditional in C++0x (or boost::conditional in Boost).

您引用的boost::mpl::if具有对应的boost::mpl::if_c,该boost::mpl::if_c采用的是bool而不是类型.这是Boost类型特征库中的常见模式.

The boost::mpl::if that you cite has a corresponding boost::mpl::if_c that takes a bool instead of a type; this is a common pattern in the Boost type traits libraries.

这篇关于是否有'choose&lt; bool,typename,typename&gt;'的标准构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆