下面的代码再现了我真的不理解的boost MPL库的行为:
#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>和其中的包含,但是代码太复杂了,我无法理解。
发布于 2012-07-19 13:22:03
返回类型的plus只保证是一个积分常量。您无法保证它的确切类型,因此您的断言被允许失败。
确切的类型如下:
mpl::plus<one,two>::type == mpl_::integral_c<int, 3>
three == foo<mpl_::int_<3> >这是不直观的。一个问题是,理论上plus<int_, int_>可以返回一个integral_c,其中第一个参数具有更大的容量,然后在溢出的情况下返回int_。
对于调试type printer可能很有用:
template<typename> print; // undefined instantiation leads to error with name
print<three> x; // nice error messagehttps://stackoverflow.com/questions/11561418
复制相似问题