【发布时间】:2018-02-18 13:00:26
【问题描述】:
在这里,C# 也有类似的问题:
Are arithmetic operations on literals in C# evaluated at compile time?,
和java:
Are arithmetic operations on literals calculated at compile time or run time?.
考虑到 C++,是否会在运行时或编译时评估以下计算?第一个是定义内置类型,第二个是作为函数参数。
但请考虑它们用于所有 4 种基本算术运算以及其他内置类型,例如一个int 而不是下面的double。
double testDouble = 2.0 + 2.0;aUserDefinedType testUserDefinedTypeObject ( aMemberVariable*std::pow(someOtherVariable, 1.0/8.0) );
【问题讨论】:
-
Go ahead and find out。这不是必需的,优化编译器可能/将会这样做。
-
诸如
2.0 + 2.0之类的常量算术表达式可能会或可能不会在编译期间进行评估。
标签: c++