我找到了一个有趣的问题,std::numeric_limits<seconds>::max()返回0.答案是使用seconds::max()或std::numeric_limits<seconds::rep>::max()替代,但我很想知道为什么会发生这种情况.我希望它在编译时失败或者只是工作.以下代码演示了gcc 4.9.3的问题.
#include <iostream>
#include <limits>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main(int /*argc*/, const char* /*argv*/[])
{
const auto maxSeconds = std::numeric_limits<seconds>::max();
std::cerr << maxSeconds.count() << "\n";
const auto maxSeconds2 = seconds::max();
std::cerr << maxSeconds2.count() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在chrono头文件中看不到任何隐式转换.如果a duration已隐式转换为数字类型并且符号丢失或者bool您最终可能最小为零 - 但最大值为零则没有意义.
正如TartanLlama指出的那样,默认特化使用默认构造函数,因此返回0.
深入研究标准的旧副本,我看到以下dictats:
18.3.2.3类模板
numeric_limits[numeric.limits]非算术标准类型,如
complex<T>(26.4.2),不应具有专业化.
过了一会儿:
默认
numeric_limits<T>模板应包含所有成员,但具有0或false值.
numeric_limitscv限定类型的特化的每个成员的值cv T应等于非限定类型的特化成员的相应成员的值T. …
在某个编译中,我需要使用-ftemplate-depth=N指定最大模板递归的选项。
是否可以从程序中获取最大模板深度的值?
gcc我对或感兴趣clang。
$ c++ -ftemplate-depth=128 main.cpp
#include<iostream>
int main(){
std::cout << MAX_TEMPLATE_RECURSION << std::endl; // hypothetical name
}
Run Code Online (Sandbox Code Playgroud)