我簡化了我的代碼生成錯誤,並發現,即使這個簡單的計數功能給了我一個錯誤(見下文):花簡單折「呼籲非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)。 –