函数不能用作“constexpr”函数

时间:2021-03-30 10:38:41

标签: c++ c++20

看看下面的代码

#include <type_traits>

template <typename T>
struct basic_type {
  using type = T;
};

consteval auto foo(auto p, auto x) noexcept {
  if constexpr (p(x)) {
    return 1;
  } else {
    return 0;
  }
}

int main() {
    // This compiles
    return foo(
        []<typename T>(basic_type<T>) 
        { 
            return std::is_integral_v<T>; 
        }, 
        basic_type<int>{});

    // This gives "x is not a constant expression"
    /*return foo(
        []<typename T>(T) 
        { 
            return std::is_integral_v<std::decay_t<T>>; 
        }, 
        0);*/
}

第一个 return 语句在最新的 gcc 主干上编译得很好,而第二个没有编译,并显示错误消息:

source>: In instantiation of 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]':
<source>:26:12:   required from here
<source>:9:3: error: 'x' is not a constant expression
    9 |   if constexpr (p(x)) {
      |   ^~
<source>: In function 'int main()':
<source>:26:19: error: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' called in a constant expression
   26 |         return foo(
      |                ~~~^
   27 |                 []<typename T>(T)
      |                 ~~~~~~~~~~~~~~~~~
   28 |                 {
      |                 ~  
   29 |                         return std::is_integral_v<std::decay_t<T>>;
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   30 |                 },
      |                 ~~ 
   31 |                 0);
      |                 ~~ 
<source>:8:16: note: 'consteval auto foo(auto:1, auto:2) [with auto:1 = main()::<lambda(T)>; auto:2 = int]' is not usable as a 'constexpr' function because:
    8 | consteval auto foo(auto p, auto x) noexcept {
      |                ^~~

谁能告诉我为什么?

这是一个godbolt链接 https://godbolt.org/z/71rbWob4e

编辑

根据要求,这里的 foo 没有自动参数:

template<typename Predicate, typename T>
consteval auto foo(Predicate p, T x) noexcept {
  if constexpr (p(x)) {
    return 1;
  } else {
    return 0;
  }
}

错误信息如下所示:


<source>: In instantiation of 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]':
<source>:27:15:   required from here
<source>:10:3: error: 'x' is not a constant expression
   10 |   if constexpr (p(x)) {
      |   ^~
<source>: In function 'int main()':
<source>:27:15: error: 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]' called in a constant expression
   27 |     return foo(
      |            ~~~^
   28 |         []<typename T>(T)
      |         ~~~~~~~~~~~~~~~~~
   29 |         {
      |         ~      
   30 |             return std::is_integral_v<std::decay_t<T>>;
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   31 |         },
      |         ~~     
   32 |         0);
      |         ~~     
<source>:9:16: note: 'consteval auto foo(Predicate, T) [with Predicate = main()::<lambda(T)>; T = int]' is not usable as a 'constexpr' function because:
    9 | consteval auto foo(Predicate p, T x) noexcept {
      | 

1 个答案:

答案 0 :(得分:19)

为了评估这一点:

  if constexpr (p(x)) {

我们需要 p(x) 是一个常量表达式。某事是否符合常量表达式的规则基于您不允许做的事情列表。

xbasic_type<int> 并且 p 是一个按值取 basic_type<int> 的函数时,我们根本就没有违反任何规则。这是一个空类型,因此复制它(正如我们在这里所做的那样)实际上并不涉及任何类型的读取。这只是有效。


但是当 xint 并且 p 是一个按值取 int 的函数时,这也需要复制 x 但这一次它涉及读取 x 的值。因为,当然,必须以某种方式初始化参数。这确实违反了一条规则:[expr.const]/8 说我们不允许执行:

<块引用>

左值到右值的转换,除非它应用于

  • 引用可用于常量表达式的对象的非易失性泛左值,或
  • 文字类型的非易失性泛左值,它指的是一个非易失性对象,其生命周期开始于 E 的计算;

当我们读取一个变量的值时会发生左值到右值的转换,而这两种情况都不适用。您实际上并不关心值是什么并不重要,因为 p 不使用它。为了甚至能够调用 p,您必须复制 x,而您不能这样做。因此出现错误。


但是,这里的 lambda 实际上并不需要值,只需要类型,因此您可以改为这样写:

    return foo(
        []<typename T>(T const&) 
        { 
            return std::is_integral_v<std::decay_t<T>>; 
        }, 
        0);

现在我们不再将 x 复制到 lambda 中,因为 lambda 不再按值获取 - 它按引用获取。因此,我们没有违反左值到右值的转换规则(或任何其他规则),现在这是一个有效的常量表达式。


然后,作为奖励,如果您将 foo 更改为引用 x(因为,您实际上并不关心该值,所以为什么不关心):

consteval auto foo(auto p, auto const& x) noexcept {
  if constexpr (p(x)) {
    return 1;
  } else {
    return 0;
  }
}

然后两个变体都变得格式错误。 basic_type<int>int 版本(无论您是按值还是按引用获取 int)。有关此案例的更多信息,请参阅我目前正在尝试使用 the constexpr array size problem 解决的 P2280

相关问题