简体   繁体   中英

How to emulate higher order function with C++ templates?

Here's a the template lessThan which works as a function.

template<int n>
struct Box
{
};

template<typename T>
struct Unbox_;

template<int n>
struct Unbox_<Box<n>>
{
    static constexpr int value = n;
};

template<typename T>
constexpr int unbox = Unbox_<T>::value;

template<typename T, typename U>
struct LessThan_
{
    static constexpr bool value = unbox<T> < unbox<U>;
};

template<typename T, typename U>
constexpr bool lessThan = LessThan_<T, U>::value;

#include <iostream>

int main()
{
    std::cout << lessThan<Box<1>, Box<2>> << '\n';
    std::cout << lessThan<Box<3>, Box<2>> << '\n';
}

I now want to do something like this

lessThan<Box<1>><Box<2>> == true

which is of course not valid C++. Why do I need this? Consider below.

template<typename T>
struct LessThanOne_
{
    static constexpr bool value = unbox<T> < 1;
};

template<typename T>
constexpr bool lessThanOne = LessThanOne_<T>::value;

In some places where I need to pass a template with one parameter, instead of passing lessThanOne , I want to pass something like lessThan<Box<1>> , so that I don't need to hardcode all cases. Is there any workaround?

You are looking for the concept calked currying. Look it up. Here's a quickly thrown together sample implementation:

template<template<class,class> class fn>
struct curry
{
  template <class A>
  struct apply1
    {
      template <class B>
      using apply = fn<A,B>;
    };
 template<class A>
 using apply = apply1<A>;
};

// That's it. Below is a test rig.

template <class>
struct test1 {};

template <template<class>class>
struct test2{};

// a meta function to test 
template <class, class>
struct myfn {};

// same function, curried 
using myfngood = curry<myfn>;

// fully applied myfngood is a type
test1 <myfngood::apply<int>::apply<char*>> t1;

// partially applied myfngood is a template
test2 <myfngood::apply<int>::apply> t2;

As RedX's comment:

template<typename RHS>
struct LessThanN {
    template <typename LHS>
    struct inner { 
        static constexpr bool value = unbox<LHS> < unbox<RHS>;
    };
};

and use it like:

template <typename LHS>
using LessThanOne = LessThanN<Box<1>>::inner<LHS>;

int main() {
    auto x = LessThanN<Box<2>>::inner<Box<1>> {};
    cout << "1<2 " << boolalpha << x.value << '\n';
    cout << "3<2 " << boolalpha << LessThanN<Box<2>>::inner<Box<3>>::value << '\n';
    cout << "0<1 " << boolalpha << LessThanOne<Box<0>>::value << '\n';
}

Having said that, your original motivational example seems to be perfectly workable if you correct the syntax error, although it doesn't deal with partial application:

template<typename T, typename U>
struct LessThan_ {
    static constexpr bool value = unbox<T> < unbox<U>;
};

template<typename T, typename U>
constexpr bool lessThan = LessThan_<T, U>::value;

static_assert(lessThan<Box<1>, Box<2>> == true, "check");
// NOT lessThan<Box<1>><Box<2>> == true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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