Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime?
由于可能在运行时调用声明为constexpr的函数,编译器在哪个条件下决定是在编译时还是在运行时计算它?
template<typename base_t, typename expo_t>
constexpr base_t POW(base_t base, expo_t expo)
{
return (expo != 0 )? base * POW(base, expo -1) : 1;
}
int main(int argc, char** argv)
{
int i = 0;
std::cin >> i;
std::cout << POW(i, 2) << std::endl;
return 0;
}
In this case, i is unknown at compile-time, which is probably the reason why the compiler treats POW() as a regular function which is called at runtime. This dynamic however, as convenient as it may appear to be, has some impractical implications. For instance, could there be a case where I would like the compiler to compute a constexpr function during compile-time, where the compiler decides to treat it as a normal function instead, when it would have worked during compile-time as well? Are there any known common pitfalls?
在这种情况下,我在编译时是未知的,这可能是编译器将POW()视为在运行时调用的常规函数的原因。然而,这种动态虽然看起来很方便,但却具有一些不切实际的含义。例如,是否存在我希望编译器在编译时计算constexpr函数的情况,编译器决定将其视为普通函数,而它在编译时也会工作?有任何已知的常见陷阱吗?
2 个解决方案
#1
72
constexpr
functions will be evaluated at compile time when all its arguments are constant expressions and the result is used in a constant expression as well. A constant expression could be a literal (like 42
), a non-type template argument (like N
in template<class T, size_t N> class array;
), an enum
element declaration (like Blue
in enum Color { Red, Blue, Green };
, another variable declared constexpr, and so on.
constexpr函数将在编译时进行评估,当它的所有参数都是常量表达式时,结果也用在常量表达式中。常量表达式可以是文字(如42),非类型模板参数(如模板中的N <类t,size_t n> 类数组;),枚举元素声明(如枚举颜色中的蓝色{红色,蓝色,绿色} ;,另一个变量声明为constexpr,依此类推。
They might be evaluated when all its arguments are constant expressions and the result is not used in a constant expression, but that is up to the implementation.
当它的所有参数都是常量表达式并且结果不在常量表达式中使用时,可能会对它们进行求值,但这取决于实现。
#2
17
The function has to be evaluated at compile-time when a constant expression is needed.
当需要常量表达式时,必须在编译时评估该函数。
The simplest method to guarantee this is to use a constexpr
value, or std::integral_constant
:
保证这一点的最简单方法是使用constexpr值或std :: integral_constant:
constexpr auto result = POW(i, 2); // this should not compile since i is not a constant expression
std::cout << result << std::endl;
or:
要么:
std::cout << std::integral_constant<int, POW(i, 2)>::value << std::endl;
or
要么
#define POW_C(base, power) (std::integral_constant<decltype(POW((base), (power)), POW((base), (power))>::value)
std::cout << POW_C(63, 2) << std::endl;
or
要么
template<int base, int power>
struct POW_C {
static constexpr int value = POW(base, power);
};
std::cout << POW_C<2, 63>::value << std::endl;
#1
72
constexpr
functions will be evaluated at compile time when all its arguments are constant expressions and the result is used in a constant expression as well. A constant expression could be a literal (like 42
), a non-type template argument (like N
in template<class T, size_t N> class array;
), an enum
element declaration (like Blue
in enum Color { Red, Blue, Green };
, another variable declared constexpr, and so on.
constexpr函数将在编译时进行评估,当它的所有参数都是常量表达式时,结果也用在常量表达式中。常量表达式可以是文字(如42),非类型模板参数(如模板中的N <类t,size_t n> 类数组;),枚举元素声明(如枚举颜色中的蓝色{红色,蓝色,绿色} ;,另一个变量声明为constexpr,依此类推。
They might be evaluated when all its arguments are constant expressions and the result is not used in a constant expression, but that is up to the implementation.
当它的所有参数都是常量表达式并且结果不在常量表达式中使用时,可能会对它们进行求值,但这取决于实现。
#2
17
The function has to be evaluated at compile-time when a constant expression is needed.
当需要常量表达式时,必须在编译时评估该函数。
The simplest method to guarantee this is to use a constexpr
value, or std::integral_constant
:
保证这一点的最简单方法是使用constexpr值或std :: integral_constant:
constexpr auto result = POW(i, 2); // this should not compile since i is not a constant expression
std::cout << result << std::endl;
or:
要么:
std::cout << std::integral_constant<int, POW(i, 2)>::value << std::endl;
or
要么
#define POW_C(base, power) (std::integral_constant<decltype(POW((base), (power)), POW((base), (power))>::value)
std::cout << POW_C(63, 2) << std::endl;
or
要么
template<int base, int power>
struct POW_C {
static constexpr int value = POW(base, power);
};
std::cout << POW_C<2, 63>::value << std::endl;