如何解决将其返回值保存在lambda函数中时,std :: ranges :: sort不能用作“ constexpr”函数
此函数f
可以将C ++ 20范围算法对象作为参数,然后使用它:
constexpr auto f(auto algo) {
return [=] {
algo(std::array{1,0});
return true;
}();
}
它可以与std::ranges::sort
一起正常工作:
static_assert(f(std::ranges::sort));
但是当我将algo
的返回值保存在lambda中时:
constexpr auto f(auto algo) {
return [=] {
auto it = algo(std::array{1,0});
return true;
}();
}
<source>:10:16: error: non-constant condition for static assertion
10 | static_assert(f(std::ranges::sort));
| ~^~~~~~~~~~~~~~~~~~~
<source>:10:16: error: 'constexpr auto f(auto:16) [with auto:16 = std::ranges::__sort_fn]' called in a constant expression
<source>:3:16: note: 'constexpr auto f(auto:16) [with auto:16 = std::ranges::__sort_fn]' is not usable as a 'constexpr' function because:
3 | constexpr auto f(auto algo) {
| ^
<source>:7:4: error: call to non-'constexpr' function 'f<std::ranges::__sort_fn>::<lambda()>'
4 | return [=] {
| ~~~~~
5 | auto it = algo(std::array{1,0});
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 | return true;
| ~~~~~~~~~~~~
7 | }();
| ~^~
<source>:4:10: note: 'f<std::ranges::__sort_fn>::<lambda()>' is not usable as a 'constexpr' function because:
4 | return [=] {
| ^
当我尝试保存constexpr
的返回值时,为什么此函数变为非'algo
'吗? 还是仅仅是个错误?
更新:以下代码为accepted by GCC,因此这很可能是一个错误,我已经提交了bug report.
constexpr auto f(auto algo,auto... args) {
return [=] () mutable {
auto it = algo(args...);
return true;
}();
}
// those are ok
static_assert(f(std::ranges::reverse,std::array{0}));
static_assert(f(std::ranges::fill,std::array{0},0));
// those are not ok
// static_assert(f(std::ranges::sort,std::array{0}));
// static_assert(f(std::ranges::replace,0));
// static_assert(f(std::ranges::unique,std::array{0}));
// static_assert(f(std::ranges::next_permutation,std::array{0}));
// static_assert(f(std::ranges::prev_permutation,std::array{0}));
解决方法
如果我声明迭代器它为constexpr,那么它对我有用:
constexpr auto it = algo(std::array{1,0});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。