为什么这个嵌套的 lambda 不被认为是 constexpr?
Why is this nested lambda not considered constexpr?
我正在尝试使用嵌套的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_()>>;
};
};
现在我们在常量表达式之外使用捕获的变量来初始化一个普通变量,然后我们可以在模板非类型参数中使用。
此答案已经过几次编辑,因此下面的评论可能会引用以前的修订版。
- 为什么std::isnan 不是 constexpr?
- 为什么rand()的使用被认为是不好的
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 为什么reinterpret_cast不是 constexpr?
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- 比较两个 constexpr 指针不是 constexpr?
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 一个C头文件可以被认为是一个接口吗
- span可以是constexpr吗
- 为什么const char*和const char[]作为函数参数被认为是等价的
- 为什么12.0==11.999999999999999999被认为是真的
- 当迭代器(输入参数)通常不是constexpr时,constexpr算法真的有用吗
- 这会被认为是糟糕的编程实践吗?
- 为什么编译器无法弄清楚构造函数实际上是 constexpr?
- const(但不是constexpr)用作内置数组大小
- "static initialization order fiasco"是 constexpr 变量的问题吗?
- 为什么这被认为是恒定的?
- 抽象类/接口中的空方法是否被认为是一种好的做法?
- 为什么这个嵌套的 lambda 不被认为是 constexpr?
- "constexpr if"被认为是SFINAE吗?