其他分享
首页 > 其他分享> > c – 在没有预处理器的情况下从hana元组创建函数签名

c – 在没有预处理器的情况下从hana元组创建函数签名

作者:互联网

有没有办法在预处理器上执行此操作?

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

namespace ba = boost::hana;

template <typename Arguments, unsigned ArgCount>
struct FunctionSigCreatorImpl {};

template <typename Arguments>
struct FunctionSigCreator : FunctionSigCreatorImpl<Arguments, decltype(ba::length(Arguments{}))::value>
{
};

#define DEF_ARG(z, n, data) \
    typename decltype(+ba::at(Arguments{}, ba::int_c<n>))::type

#define DEF_FUN_CREATOR(z, argCount, data)                                                  \
    template <typename Arguments>                                                         \
    struct FunctionSigCreatorImpl<Arguments, argCount>                                    \
    {                                                                                       \
        using Type = void(BOOST_PP_ENUM(argCount, DEF_ARG,));      \
    };

BOOST_PP_REPEAT(19, DEF_FUN_CREATOR,)

int main(int argc, char **argv)
{
    using MyTuple = ba::tuple<ba::type<int>, ba::type<long>, ba::type<char>>;
    static_assert(std::is_same<typename FunctionSigCreator<MyTuple>::Type, void(int, long, char)>::value);

    return 0;
}

解决方法:

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

namespace ba = boost::hana;

template<typename ... Types>
ba::type<void(typename Types::type...)> FunctionSigCreatorImpl(ba::tuple<Types...>){ return{}; }

template<typename Tuple>
using FunctionSigCreator = decltype(FunctionSigCreatorImpl(std::declval<Tuple>()));

int main()
{
    using MyTuple = ba::tuple<ba::type<int>, ba::type<long>, ba::type<char>>;
    //using MyTuple = decltype(ba::tuple_t<int,long,char>);
    static_assert(std::is_same<typename FunctionSigCreator<MyTuple>::type, void(int, long, char)>::value);

    return 0;
}

标签:boost-hana,c,boost
来源: https://codeday.me/bug/20190828/1748299.html