我试图使用嵌套的constexpr lambda创建柯里化接口,但编译器认为它不是常量表达式。
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不是常量表达式
:如果有人能解释一下这个错误,那就太好了。我之前也遇到过这个错误,但是不记得为什么了。
size
是一个非constexpr函数参数。有一个简单的解决方法。询问为什么lambda调用的结果不是constexpr,也许他们会取消标记它。 - Jason Riceconstexpr auto size_ = decltype(size){};
或使用类型别名并在没有捕获的嵌套lambda上下文中使用Size{}
,这更好。请注意,事先将所有内容都设置为constexpr并不总是必要的。 - Jason Rice