下面的代码再现了一个我真的不理解 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
的类型不同?
我在尝试解决第 3 章末尾的练习时遇到了这个问题
C++ Template Meta-Programming .我已经试过偷看 <boost/mpl/plus.hpp>
以及其中的内容,但是代码太复杂了,我看不懂。
最佳答案
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 message
关于c++ - `mpl::plus< mpl::int_<1>, mpl::int_<2>>::type` 与 `mpl::int_<3>` 不是同一类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561418/