admin管理员组文章数量:1332881
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?
本文标签: cHow can I implement a function to call any templated lambda functionStack Overflow
版权声明:本文标题:c++ - How can I implement a function to call any templated lambda function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304797a2449702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论