常量表达式问题生成 boost::hana::set

Constant expression issue generating boost::hana::set

我正在尝试使用 boost::hana 创建一组独特的类型,并且需要将所有内容都保留为 constexprs。

我的尝试如下:

#include <iostream>
#include <boost/hana.hpp>

namespace hana = boost::hana;

template < class ...Xs >
constexpr auto get_set( Xs &&...xs)
{
    constexpr auto f = []( auto set, auto element )
    {
        using Type = decltype( element );
        return hana::insert( set, hana::type_c< Type > );
    };

    constexpr auto t = hana::make_tuple( std::forward<Xs>(xs)...);
    return hana::fold_left( t, hana::make_set( ),  f);
}

int main()
{
    constexpr int i(0);
    constexpr double j(0);
    constexpr auto set = get_set( i, j, i);
}

这给我以下错误:

<source>: In instantiation of 'constexpr auto get_set(Xs&& ...) [with Xs = {const int&, const double&, const int&}]':    
<source>:23:46:   required from here    
<source>:15:44:   in 'constexpr' expansion of 'boost::hana::make_tuple.boost::hana::make_t<boost::hana::tuple_tag>::operator()<const int&, const double&, const int&>((* & std::forward<const int&>((* & xs#0))), (* & std::forward<const double&>((* & xs#1))), (* & std::forward<const int&>((* & xs#2))))'    
/opt/compiler-explorer/libs/boost_1_65_0/boost/hana/fwd/core/make.hpp:61:41:   in 'constexpr' expansion of 'boost::hana::make_impl<boost::hana::tuple_tag>::apply<const int&, const double&, const int&>(x#0, x#1, x#2)'    
<source>:15:24:   in 'constexpr' expansion of 'boost::hana::tuple<int, double, int>(xs#0, xs#1, xs#2)'    
/opt/compiler-explorer/libs/boost_1_65_0/boost/hana/tuple.hpp:112:29:   in 'constexpr' expansion of '((boost::hana::tuple<int, double, int>*)this)->boost::hana::tuple<int, double, int>::storage_.boost::hana::basic_tuple<int, double, int>::basic_tuple<const int&, const double&, const int&>(xn#0, xn#1, xn#2)'    
/opt/compiler-explorer/libs/boost_1_65_0/boost/hana/basic_tuple.hpp:89:44:   in 'constexpr' expansion of '((boost::hana::basic_tuple<int, double, int>*)this)->boost::hana::basic_tuple<int, double, int>::<anonymous>.boost::hana::detail::basic_tuple_impl<std::integer_sequence<long unsigned int, 0, 1, 2>, int, double, int>::basic_tuple_impl<const int&, const double&, const int&>(yn#0, yn#1, yn#2)'    
/opt/compiler-explorer/libs/boost_1_65_0/boost/hana/basic_tuple.hpp:62:65:   in 'constexpr' expansion of '((boost::hana::detail::basic_tuple_impl<std::integer_sequence<long unsigned int, 0, 1, 2>, int, double, int>*)this)->boost::hana::detail::basic_tuple_impl<std::integer_sequence<long unsigned int, 0, 1, 2>, int, double, int>::<anonymous>._hana::ebo<boost::hana::detail::bti<0>, int, false>::ebo<const int&>(yn#0)'    
<source>:15:24: error: 't' is not a constant expression    
         constexpr auto t = hana::make_tuple( std::forward<Xs>(xs)...);    
                        ^    
Compiler returned: 1

神箭link.

如果我在 int main.

中禁用 constexprs,代码就可以工作

有什么想法吗? 谢谢!

函数参数不是常量表达式,即使在 constexpr 函数中也是如此。所以这个:

constexpr auto t = hana::make_tuple( std::forward<Xs>(xs)...);

无法工作。您正在尝试制作一个 constexpr 元组,但使用 xs... 不会给您一个常量表达式。由于您都关心类型,所以这有效:

constexpr auto t = hana::make_tuple(Xs{}...);

或者,出于任何原因,您实际上并不需要 t 本身是 constexpr,因此您可以删除它:

auto t = hana::make_tuple(std::forward<Xs>(xs)...);

别担心。思考在 constexpr 土地上什么行得通什么行不通是……具有挑战性的。