gpt4 book ai didi

c++ - 生成 boost::hana::set 的常量表达式问题

转载 作者:行者123 更新时间:2023-11-28 04:31:55 26 4
gpt4 key购买 nike

我正在尝试使用 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 中禁用 constexpr,代码就可以工作。

有什么想法吗?谢谢!

最佳答案

函数参数不是常量表达式,即使在 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 领域思考什么有效,什么无效是……具有挑战性的。

关于c++ - 生成 boost::hana::set 的常量表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52654225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com