是否有“choose”的标准构造?
我有时发现自己需要以下内容:
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 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;
};
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. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的:它在 C++0x 中称为
std::conditional
(或在 Boost 中称为boost::conditional
)。您引用的
boost::mpl::if
具有相应的boost::mpl::if_c
,它采用bool
而不是类型;这是 Boost 类型特征库中的常见模式。Yes: it is called
std::conditional
in C++0x (orboost::conditional
in Boost).The
boost::mpl::if
that you cite has a correspondingboost::mpl::if_c
that takes abool
instead of a type; this is a common pattern in the Boost type traits libraries.