我假设 BOOST_STRONG_TYPEDEF 定义类型上的 numeric_limits::max() 会给出与基础类型相同的结果。但下面的程序表明情况并非如此(用g++或clang++与boost 1.65编译):
#include <boost/serialization/strong_typedef.hpp>
#include <limits>
#include <iostream>
BOOST_STRONG_TYPEDEF(uint64_t, s);
int main(int , char **)
{
std::cerr << std::numeric_limits<uint64_t>::max() << std::endl
<< std::numeric_limits<s>::max() << std::endl;
return 0;
}
结果:
$ clang++ test.cpp
$ ./a.out
18446744073709551615
0
这是预期的结果吗?
最佳答案
I was supposing that numeric_limits::max() on a BOOST_STRONG_TYPEDEF defined type would give the same result than on the underlying type.
没有理由这样认为。
您实际上是在要求一种新类型。这就是拥有强 typedef 的意义。这就是为什么您使用 BOOST_STRONG_TYPEDEF
而不是 typedef
。
std::numeric_limits
不会、不能也不应该为它不知道的类型(例如您创建的新类型)提供有意义的信息。
关于c++ - BOOST_STRONG_TYPEDEF 上的 numeric_limits 是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58819578/