整数的指数 Shorthand
Exponential Shorthand for Integers
所以我的理解是:1e3
等同于 1000.0
。
我的问题是,整数是否有类似的 shorthand?我意识到我可以做到:static_cast<int>(1e3)
。除了这个我还有什么可用的吗?
不,C++ 中没有科学计数整数文字的语法。
您可以使用用户定义的文字来缩短转换时间:
constexpr int operator "" _i(long double d) noexcept {
return d;
}
int main() {
auto big = 1e3_i; // is int
}
但是,这确实(至少在我测试过的 GCC 上)防止编译器注意到初始化中的溢出,因此在可能的情况下,更喜欢更传统的方法:
int big = 1e30; // compiler should yell at you
所以我的理解是:1e3
等同于 1000.0
。
我的问题是,整数是否有类似的 shorthand?我意识到我可以做到:static_cast<int>(1e3)
。除了这个我还有什么可用的吗?
不,C++ 中没有科学计数整数文字的语法。
您可以使用用户定义的文字来缩短转换时间:
constexpr int operator "" _i(long double d) noexcept {
return d;
}
int main() {
auto big = 1e3_i; // is int
}
但是,这确实(至少在我测试过的 GCC 上)防止编译器注意到初始化中的溢出,因此在可能的情况下,更喜欢更传统的方法:
int big = 1e30; // compiler should yell at you