【发布时间】:2020-12-10 10:06:46
【问题描述】:
我正在使用一个外部库,它定义了一个带有 unsigned int C 样式数组的结构:
struct Foo
{
unsigned int bar[8];
}
在我的代码中,我想获取该类型的 numeric_limits::max() 以检查超出范围的值,并避免将溢出的值传递给库。
所以我这样做了:
auto n = Foo().bar[0];
if(myBar > std::numeric_limits<decltype (n)>::max())
{
error("The Foo library doesn't support bars that large");
return false;
}
这可行,但有没有更优雅的 c++11 方式不暗示声明变量?如果我使用decltype(Foo().bar[0]),我有一个错误,因为它返回一个引用类型,numeric_limits 不喜欢。
【问题讨论】:
标签: c++ c++11 templates types decltype