简体   繁体   English

C ++模板模板非类型参数

[英]C++ template template non-type parameter

I am trying to achieve the following: 我想要实现以下目标:

template<template<typename> bool Function_, typename ... Types_>
constexpr auto find(Tuple<Types_ ... >) noexcept
{
    // ... 
}

where a possible function could be: 可能的功能可能是:

template<typename T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;

so then the usage of find would be: 那么find的用法是:

Tuple<int, char, void *> t;
find<is_pointer_v>(t);

don't worry about the implementation of find, I am just asking about how to do " template < typename > bool Function_ " as the bool part is invalid in c++ currently. 不要担心find的实现,我只是询问如何做“ template < typename > bool Function_ ”,因为bool部分目前在c ++中无效。

any help is appreciated! 任何帮助表示赞赏!

EDIT: 编辑:

here is an example of why I can't pass the " is_pointer " to the function: 这是一个为什么我不能将“ 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);
}

this produces the compiler error: 这会产生编译器错误:

error: no matching function for call to 'apply(< unresolved overloaded function type >, sigma::meta::Type&)' apply(add_pointer, t_i); 错误:没有匹配函数调用'apply(<unresolved overloaded function type>,sigma :: meta :: Type&)'apply(add_pointer,t_i);

any help is appreciated! 任何帮助表示赞赏!

You can simply wrap your functions within functors. 您可以简单地将函数包装在仿函数中。
As a minimal, working example: 作为一个最小的工作示例:

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);
}

If you can't change them directly, create functors that forward everything to the right function through a static constexpr member method. 如果您无法直接更改它们,请创建函数,通过静态constexpr成员方法将所有内容转发到正确的函数。

I am just asking about how to do "template < typename > bool Function_" as the bool part is invalid in c++ currently. 我只是询问如何做“模板<typename> bool Function_”,因为bool部分目前在c ++中无效。

As far I know, template-template arguments are a completely different thing. 据我所知,模板模板参数完全不同。 They are intended for containers, not for functions. 它们用于容器,而不是用于功能。 So class , not bool . 所以class ,而不是bool

here is an example of why I can't pass the "is_pointer" to the function 这是一个为什么我不能将“is_pointer”传递给函数的示例

Your example doesn't work because add_pointer is a template function, so when you call 您的示例不起作用,因为add_pointer是一个模板函数,所以当您调用时

apply(add_pointer, t_i);

the compiler doesn't know which version (which type T ) of add_pointer to use. 编译器不知道要使用哪个版本(哪个类型T )的add_pointer

A solution can be explicit it, as in the following simplified example 解决方案可以是明确的,如下面的简化示例所示

#include <tuple>
#include <iostream>

template <typename T>
constexpr auto add_pointer(std::tuple<T>) noexcept
 { std::cout << "add_pointer" << std::endl; return 0; }

template <typename F, typename T>
constexpr auto apply(F f, std::tuple<T> t) noexcept
 { return f(t); }

int main(void)
 {
   std::tuple<int> t_i { 1 };

   apply<int(*)(std::tuple<int>)>(add_pointer, t_i);
 }

but I understand that explicating int(*)(std::tuple<int>) is a big pain in the ass. 但我明白,解释int(*)(std::tuple<int>)是一个很大的痛苦。

You can simplify a little using the fact that you pass t so you can deduce the type of the argument received by the function, but (for a generic solution) I don't know how to avoid to explicit the return type of the function (maybe it's possible, but (in this moment) I don't know. 您可以使用传递t的事实简化一点,这样您就可以推断出函数接收的参数的类型,但是(对于通用解决方案)我不知道如何避免显式函数的返回类型(也许这是可能的,但(在这一刻)我不知道。

So you can simplify the call as follows 因此,您可以按如下方式简化呼叫

apply<int>(add_pointer, t_i);

and the following is a little more general example 以下是一个更一般的例子

#include <tuple>
#include <iostream>

template <typename ... Ts>
constexpr auto add_pointer(std::tuple<Ts...> const &) noexcept
 { std::cout << "add_pointer" << std::endl; return 0; }

template <typename R, typename ... Ts, 
          typename F = R(*)(std::tuple<Ts...> const &)>
constexpr auto apply(F f, std::tuple<Ts...> t) noexcept
 { return f(t); }

int main(void)
 {
   std::tuple<int> t_i { 1 };

   apply<int>(add_pointer, t_i);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM