c++ - How can I implement a function to call any templated lambda function? - Stack Overflow

I have a value_t to wrap a value as a type:template <auto _v>struct value_t { static constexpr

I have a value_t to wrap a value as a type:

template <auto _v>
struct value_t { static constexpr auto value = _v; };

I can use take_off to extract the type and value:

template <typename T>
struct take_off { using magic = T; };
template <auto _v>
struct take_off<value_t<_v>> { static constexpr auto magic = _v; };

Now I want to call any lambda function with any template arguments and function arguments by calling invoke(c++20 or 23):

auto test_fn = []<typename T, typename T2, T... Vs>(int a, int b){
    (..., (std::cout << (T2)Vs << std::endl));
    std::cout << a << b << std::endl;
};

// template <typename...>
// auto invoke(test_fn, ...)

invoke<int, double, value_t<1>, value_t<2>, value_t<5>>(test_fn, 1, 2);

My implementation of invoke is:

template <typename... Ts>
decltype(auto) invoke(auto _fn, auto... _args) {
    return _fn.template operator()<take_off<Ts>::magic...>(_args...);
}

It seems to works well on the MSVC, due to msvc's extended standard. However, an error has occurred when it is compiled by clang:

error: missing 'typename' prior to dependent type name `take_off<Ts>::magic`

invoke will no longer work for auto template arguments if I use typename take_off<Ts>::magic instead.

So is there any way to implement this invoke?

For example, if we limit template parameters to a maximum of N, here we need 8.

A bad implementation would be to enumerate all cases by judging the type or value flag of each magic and rounding null flag to 8, which would require implementing 3^8 functions.

template <>
auto invoke_impl<t, t, v, v, v, null, null, null> {...}

Is there a way to simplify this process or is there another easier way to do it?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742304797a4418725.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信