Fle*_*ght 5 c++ templates template-templates non-type c++17
我想要实现以下目标:
template<template<typename> bool Function_, typename ... Types_>
constexpr auto find(Tuple<Types_ ... >) noexcept
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
可能的功能可能是:
template<typename T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;
Run Code Online (Sandbox Code Playgroud)
那么find的用法是:
Tuple<int, char, void *> t;
find<is_pointer_v>(t);
Run Code Online (Sandbox Code Playgroud)
不要担心find的实现,我只是询问如何做" template < typename > bool Function_"因为bool当前c ++中的部分无效.
任何帮助表示赞赏!
编辑:
这是一个为什么我不能将" is_pointer" 传递给函数的例子:
template<typename T_>
constexpr auto add_pointer(Type<T_>) noexcept
{ return type_c<T_ *>; }
template<typename F_, typename T_>
constexpr auto apply(F_ f, Type<T_> t) noexcept
{
return f(t);
}
int main(void)
{
Type<int> t_i;
apply(add_pointer, t_i);
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译器错误:
错误:没有匹配函数调用'apply(<unresolved overloaded function type>,sigma :: meta :: Type&)'apply(add_pointer,t_i);
任何帮助表示赞赏!
您可以简单地将函数包装在函子中。
作为一个最小的工作示例:
template<typename>
struct Type {};
template<typename>
struct type_c {};
template<typename T_>
struct add_pointer {
static constexpr auto invoke(Type<T_>) noexcept
{ return type_c<T_ *>{}; }
};
template<template<typename> class F_, typename T_>
constexpr auto apply(Type<T_> t) noexcept {
return F_<T_>::invoke(t);
}
int main(void) {
Type<int> t_i;
apply<add_pointer>(t_i);
}
Run Code Online (Sandbox Code Playgroud)
如果无法直接更改它们,请创建函子,通过静态 constexpr 成员方法将所有内容转发到正确的函数。