#include <limits>
#include <cstdint>
#include <iostream>
template<typename T>
T f(T const a = std::numeric_limits<T>::min(),
T const b = std::numeric_limits<T>::max())
{
if (a >= b)
{
throw 1;
}
auto n = static_cast<std::uint64_t>(b - a + 1);
if (0 == n)
{
n = 1;
}
return n;
}
int main()
{
std::cout << f<int>() << std::endl;
}
g++-11 -std=c++20 -O2
0
应该输出1
!
clang++ 符合预期。如果我-O2
改为-O0
,g++-11 也可以。
参见:在线演示
为什么 g++在正常的情况下 -O2
包含错误 -O0
?