2017-06-23 40 views
1

我簡化了我的代碼生成錯誤,並發現,即使這個簡單的計數功能給了我一個錯誤(見下文):花簡單折「呼籲非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,但它看起來像它只使用常量值。任何人都可以告訴我如何讓這段代碼工作? (它的目的是計算傳遞給元組的元素的數量)

+0

['[](...)constexpr {...}'](https://isocpp.org/files/papers/P0170R1.pdf)? –

+0

@HenriMenke'錯誤:期望'{'在'g ++ ++ 6.3之前的'constexpr'上(雖然在clang上工作,但是在clang我提供的代碼原樣) – Whyyy

+0

_constexpr lambda表達式_ [僅在GCC 7之後受支持](http: //en.cppreference.com/w/cpp/compiler_support)。 –

回答

0

如果你的編譯器不支持constexpr lambdas,你必須自己寫出閉包類型(在命名空間或類作用域,而不是函數因爲本地類範圍內不能有成員模板):

constexpr struct inc_t { 
    template<class N, class T> 
    constexpr auto operator()(N n, T) const { return hana::int_c<n> + hana::int_c<1>; } 
} inc; 

否則,你仍然可以使用,但花計算的結果將不會提供給類型系統。

+0

實際上,參數'n'有錯誤。 'hana :: int_c '應該是'n'。 –

+0

@奧尼爾是的,那會更簡單。 'hana :: int_c '仍然有效,因爲'hana :: integral_constant'繼承自'std :: integral_constant',所以有一個constexpr轉換爲'int'。 – ecatmur

+0

這不是關於轉換,而是函數參數進入模板參數。 –