我简化了我的代码生成错误,并发现,即使这个简单的计数功能给了我一个错误(见下文):花简单折“呼吁非constexpr功能”
#include <boost/hana/tuple.hpp>
#include <boost/hana/fold.hpp>
#include <boost/hana/plus.hpp>
#include <boost/hana/integral_constant.hpp>
int main() {
using namespace boost;
constexpr auto inc = [](auto n, auto el) { return hana::int_c<n> + hana::int_c<1>; };
constexpr auto count = hana::fold(hana::make_tuple(hana::int_c<3>),
hana::int_<0>{},
inc
);
return 0;
}
错误(ommitted一些这似乎无关):
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:202:57: error: ‘static constexpr decltype(auto) boost::hana::detail::variadic::foldl1_impl<2u>::apply(F&&, X1&&, X2&&) [with F = const main()::<lambda(auto:1, auto:2)>&; X1 = boost::hana::integral_constant<int, 0>; X2 = boost::hana::integral_constant<int, 3>]’ called in a constant expression
return foldl1_impl<sizeof...(xn) + 1>::apply(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
static_cast<F&&>(f), static_cast<X1&&>(x1), static_cast<Xn&&>(xn)...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
);
~
In file included from /usr/local/include/boost/hana/fold_left.hpp:18:0,
from /usr/local/include/boost/hana/concept/foldable.hpp:19,
from /usr/local/include/boost/hana/core/to.hpp:16,
from /usr/local/include/boost/hana/bool.hpp:17,
from /usr/local/include/boost/hana/tuple.hpp:16,
from ...:
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:31:41: note: ‘static constexpr decltype(auto) boost::hana::detail::variadic::foldl1_impl<2u>::apply(F&&, X1&&, X2&&) [with F = const main()::<lambda(auto:1, auto:2)>&; X1 = boost::hana::integral_constant<int, 0>; X2 = boost::hana::integral_constant<int, 3>]’ is not usable as a constexpr function because:
static constexpr decltype(auto) apply(F&& f, X1&& x1, X2&& x2) {
^~~~~
/usr/local/include/boost/hana/detail/variadic/foldl1.hpp:32:39: error: call to non-constexpr function ‘main()::<lambda(auto:1, auto:2)> [with auto:1 = boost::hana::integral_constant<int, 0>; auto:2 = boost::hana::integral_constant<int, 3>]’
return static_cast<F&&>(f)(static_cast<X1&&>(x1),
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
static_cast<X2&&>(x2));
我用C++ 17与g ++ 6.3。它看起来像是说lambda不是一个constexpr,但它看起来像它只使用常量值。任何人都可以告诉我如何让这段代码工作? (它的目的是计算传递给元组的元素的数量)
['[](...)constexpr {...}'](https://isocpp.org/files/papers/P0170R1.pdf)? –
@HenriMenke'错误:期望'{'在'g ++ ++ 6.3之前的'constexpr'上(虽然在clang上工作,但是在clang我提供的代码原样) – Whyyy
_constexpr lambda表达式_ [仅在GCC 7之后受支持](http: //en.cppreference.com/w/cpp/compiler_support)。 –