2017-06-23 42 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

这不是关于转换,而是函数参数进入模板参数。 –