Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

any help is appreciated!

EDIT:

here is an example of why I can't pass the "is_pointer" to the function:

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

like image 654
Fletcher Blight Avatar asked Jul 12 '17 04:07

Fletcher Blight


1 Answers

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.

like image 162
skypjack Avatar answered Sep 20 '22 08:09

skypjack