首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`mpl::plus< mpl::int_<1>,mpl::int_<2> >:: type `与`mpl::int_<3>`的类型不同?

`mpl::plus< mpl::int_<1>,mpl::int_<2> >:: type `与`mpl::int_<3>`的类型不同?
EN

Stack Overflow用户
提问于 2012-07-19 12:59:23
回答 1查看 438关注 0票数 4

下面的代码再现了我真的不理解的boost MPL库的行为:

代码语言:javascript
复制
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/plus.hpp>

using namespace boost;

int main() {
  typedef mpl::int_<1> one;
  typedef mpl::int_<2> two;
  typedef mpl::int_<3> three;
  // The following line breaks compilation...
  // static_assert( is_same< mpl::plus<one,two>::type, three >::type::value, "Not the same type");
  // ...while this works
  static_assert( mpl::plus<one,two>::type::value == three::value , "Not the same value");
  return 0;
}

我的问题是:为什么mpl::plus<one,two>::type three**?** 与不同?

我在试图解决C++模板元编程第3章末尾的练习时遇到了这个问题。我已经试着查看过<boost/mpl/plus.hpp>和其中的包含,但是代码太复杂了,我无法理解。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-19 13:22:03

返回类型的plus只保证是一个积分常量。您无法保证它的确切类型,因此您的断言被允许失败。

确切的类型如下:

代码语言:javascript
复制
mpl::plus<one,two>::type == mpl_::integral_c<int, 3>
three == foo<mpl_::int_<3> >

这是不直观的。一个问题是,理论上plus<int_, int_>可以返回一个integral_c,其中第一个参数具有更大的容量,然后在溢出的情况下返回int_

对于调试type printer可能很有用:

代码语言:javascript
复制
template<typename> print; // undefined instantiation leads to error with name
print<three> x; // nice error message
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11561418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档