ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在C中制作基于SFINAE的Y组合器?

2019-08-30 08:08:38  阅读:309  来源: 互联网

标签:c templates c14 sfinae typetraits


我正在考虑C 14的隐式模板,我正在尝试声明一个函数来匹配特定的参数类型(SFINAE和特征仍让我头疼).我不知道如何解释我想要的东西,但我正在尝试制作一个Y combinator(只是为了看看它是否可行,不是用于制作).

我正在尝试声明一个函数:

template<typename T>
my_traits<T>::return_type Y(T t) {
  // ...
};

这样T就是匹配的函数(或函子)

std::function<R(F, Args...)>

// where F (and above return_type) will be
std::function<R(Args...)>

哪个会占用任意数量的参数,但第一个应该是一个具有相同返回类型和相同参数的函数(除了这个函数本身).仿函数的operator()的第一个参数是模板.

我想要实现的用法:

auto fib = [](auto myself, int x) {
  if(x < 2)
    return 1;
  return myself(x - 1) + myself(x - 2);
};

// The returned type of fib should be assignable to std::function<int(int)>

我无法获取T类型的返回类型(因为重载的operator()).我想要做的是什么?我该怎么做?

编辑:

从不同的角度看,我正在努力做到这一点:

struct my_functor {
  template<typename T>
  char operator () (T t, int x, float y) { /* ... */ };
};

template<typename T>
struct my_traits {
  typedef /* ... */ result_type;

  /* ... */
};

// I want this to be std::function<char(int, float)>, based on my_functor
using my_result =
my_traits<my_functor>::result_type;

解决方法:

在C 14返回类型推导中,不可能像int期望那样从int(T,int)中推导出int(int).

但是,我们可以使用以下方法屏蔽结果的第一个参数. struct YCombinator使用非递归函数对象成员进行实例化,其第一个参数是没有第一个参数的自身版本. YCombinator提供了一个调用操作符,它接收非递归函数的参数,然后在替换第一个参数后返回其函数对象成员.这种技术允许程序员在递归函数的定义中避免我自己(我自己,……)调用的混乱.

template<typename Functor>
struct YCombinator
{
    Functor functor;

    template<typename... Args>
    decltype(auto) operator()(Args&&... args)
    {
        return functor(*this, std::forward<Args>(args)...);
    }
};

make_YCombinator实用程序模板允许简化的使用模式.这个编译运行在GCC 4.9.0中运行.

template<typename Functor>
decltype(auto) make_YCombinator(Functor f) { return YCombinator<Functor> { f }; }

int main()
{
    auto fib = make_YCombinator([](auto self, int n) -> int { return n < 2 ? 1 : self(n - 1) + self(n - 2); });

    for (int i = 0; i < 10 ; ++i)
        cout << "fib(" << i << ") = " << fib(i) << endl;

    return 0;
}

由于在定义递归函数时未定义非递归函数,通常递归函数必须具有显式返回类型.

编辑:

但是,如果程序员在使用非递归函数之前注意指示递归函数的返回类型,则编译器可能在某些情况下推断返回类型.虽然上面的构造需要一个显式的返回类型,但在下面的GCC 4.9.0中推断返回类型没有问题:

    auto fib = make_YCombinator([](auto self, int n) { if (n < 2) return 1; return self(n - 1) + self(n - 2); });

为了进一步确定这一点,这里引用了关于退货类型扣除[C 7.1.6.4.11]的C 14标准草案:

If the type of an entity with an undeduced placeholder type is needed
to determine the type of an expression, the program is ill-formed.
Once a return statement has been seen in a function, however, the
return type deduced from that statement can be used in the rest of the
function, including in other return statements. [ Example:

06003

—end example ]

标签:c,templates,c14,sfinae,typetraits
来源: https://codeday.me/bug/20190830/1766571.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有