以下代码在 Visual Studio 2013 而非 gcc 4.9.2 下运行时会引发异常。
报告的错误是:
'例外:stol 参数超出范围'
stol
返回 along
所以大小temp
应该足够大以容纳返回的值。
任何人都可以解释这种行为。这可能是编译器错误吗?
#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
int main()
{
const std::string value = "4294967295"; // 0xffffffff
try
{
int64_t temp = std::stol(value);
}
catch (std::invalid_argument& ex)
{
std::cout << "invalid_argument: " << ex.what() << "\n";
}
catch (std::exception& ex)
{
std::cout << "exception: " << ex.what() << "\n";
}
return 0;
}