为什么这个嵌套的 lambda 不被认为是 constexpr?

Why is this nested lambda not considered constexpr?

本文关键字:认为是 constexpr lambda 嵌套 为什么      更新时间:2023-10-16

我正在尝试使用嵌套的constexpr lambdas创建一个柯里德接口,但编译器不认为它是一个常量表达式。

namespace hana = boost::hana;
using namespace hana::literals;
struct C1 {};
template < typename T,
std::size_t size >
struct Array {};
constexpr auto array_ = [] (auto size) {
return [=] (auto type) {
return hana::type_c<Array<typename decltype(type)::type, size()>>;
};
};
int main() {
constexpr auto c1 = hana::type_c<C1>;
constexpr auto test = hana::type_c<Array<typename decltype(c1)::type, hana::size_c<100>()>>;
constexpr auto test2 = array_(hana::size_c<100>)(c1);
}

我之前发布了一个问题,因为我发现了一个不同的最小示例,但这还不够。

错误:

test2.cpp: In instantiation of ‘<lambda(auto:1)>::<lambda(auto:2)> [with auto:2 = boost::hana::type_impl<C1>::_; auto:1 = boost::hana::integral_constant<long unsigned int, 100>]’:
test2.cpp:31:54:   required from here
test2.cpp:20:16: error: ‘__closure’ is not a constant expression
return hana::type_c<Array<typename decltype(type)::type, size()>>;
^~~~
test2.cpp:20:16: note: in template argument for type ‘long unsigned int’ 
test2.cpp: In function ‘int main()’:
test2.cpp:31:18: error: ‘constexpr const void test2’ has incomplete type
constexpr auto test2 = array_(hana::size_c<100>)(c1);

__closure is not a constant expression:如果有人能向我解释这个错误,那将是一个很大的帮助。我之前遇到过这个错误,但不记得为什么。

我将您的测试用例简化为:

#include <type_traits>
constexpr auto f = [](auto size) {
return [=](){
constexpr auto s = size();
return 1;
};
};
static_assert(f(std::integral_constant<int, 100>{})(), "");
int main() { }

如上面的注释中所述,发生这种情况是因为size不是函数体内的常量表达式。这不是哈娜特有的。作为解决方法,您可以使用

constexpr auto f = [](auto size) {
return [=](){
constexpr auto s = decltype(size)::value;
return 1;
};
};

或类似的东西。

问题是您正在尝试在模板非类型参数中使用 lambda 捕获的变量之一。

return hana::type_c<Array<typename decltype(type)::type, size()>>;
//                                                         ^~~~

模板非类型参数必须是常量表达式。在 lambda 中,您不能在常量表达式中使用捕获的变量。λ是否constexpr无关紧要。

但是您可以在常量表达式中使用普通变量,即使它们不是constexpr变量。例如,这是合法的:

std::integral_constant<int, 100> i; // i is not constexpr
std::array<int, i()> a; // using i in a constant expression

那么为什么我们不能在常量表达式中使用捕获的变量呢?我不知道这个规则的动机,但它在标准中:

[EXPR.const]

(¶2)条件表达式是核心常量表达式,除非... (¶2.11) 在 lambda 表达式中,对this的引用或对该lambda 表达式外部定义的具有自动存储持续时间的变量的引用,其中引用将是 ODR 使用。

CWG1613可能掌握了一些线索。

如果我们要将内部 lambda 重写为命名类,我们将遇到一个不同但相关的问题:

template <typename T>
struct Closure {
T size;
constexpr Closure(T size_) : size(size_) {}
template <typename U>
constexpr auto operator()(U type) const {
return hana::type_c<Array<typename decltype(type)::type, size()>>;
}
};
constexpr auto array_ = [] (auto size) {
return Closure { size };
};

现在的错误是在模板非类型参数中隐式使用this指针。

return hana::type_c<Array<typename decltype(type)::type, size()>>;
//                                                         ^~~~~

为了保持一致性,我声明Closure::operator()()是一个constexpr函数,但这无关紧要。禁止在常量表达式中使用this指针 ([expr.const] ¶2.1)。声明constexpr函数没有特殊的豁免来放宽其中可能出现的常量表达式的规则。

现在原始错误更有意义了,因为捕获的变量被转换为 lambda 闭包类型的数据成员,因此使用捕获的变量有点像通过 lambda 自己的"this指针"进行间接处理。

这是对代码进行最少更改的解决方法:

constexpr auto array_ = [] (auto size) {
return [=] (auto type) {
const auto size_ = size;
return hana::type_c<Array<typename decltype(type)::type, size_()>>;
};
};

现在我们在常量表达式之外使用捕获的变量来初始化一个普通变量,然后我们可以在模板非类型参数中使用。

此答案已经过几次编辑,因此下面的评论可能会引用以前的修订版。