是否有使用 lambda 语法定义递归 constexpr 函数的便捷方法?我找到了一种通过分配给函数指针的不方便的方法constexpr,但我想要一种方法来减少输入并且不更改 lambda 的类型。
以普通方式创建递归 constexpr 函数非常简单。特别是,从 C++11 开始支持使用可能包含三元运算符的单个表达式。
constexpr double factorial2(double x) {
return (x < 0) ? 0 : \
(x == 0) ? 1 : \
/*otherwise*/ (x * factorial2(-1 + x));
}
Run Code Online (Sandbox Code Playgroud)
不太清楚如何使用 lambda 语法来做到这一点。我将在下面包括我的各种失败尝试,但我找到了一种方法,通过使用constexpr函数指针而不是auto作为我正在用我的 lambda 初始化的变量的类型注释来创建 constexpr 函数。
typedef double (* factorial_t)(double);
constexpr factorial_t factorial = [](double x) constexpr noexcept -> double {
return (x < 0) ? 0 : \
(x == 0) …Run Code Online (Sandbox Code Playgroud) 我找到了一篇关于如何创建递归 lambda的帖子,但不清楚如何从函数中返回它。
据我所知,在下面的代码中,捕获func是指被销毁的对象:
#include <iostream>
#include <functional>
std::function<int (int)> make_lambda()
{
std::function<int (int)> func;
func = [&func](int val)
{
if (val < 10)
{
return func(val + 1);
}
return val;
};
return func;
}
int main()
{
std::cout << make_lambda()(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何使这段代码工作?
有没有比使用更好的方法std::shared_pointer<std::function<int (int)>>?
void make_lambda(std::function<int (int)>&) 不是一个选择。
编辑1:
为什么this不允许 lambdas 引用自己?
我正在尝试构建一个具有递归能力的 lambda 自作用域的干净整洁的实现(这基本上是一个 Y 组合器,尽管我认为技术上不完全)。这是一个旅程,它带我到许多其他人中,这个线程,这个线程和这个线程。
我已经尽可能干净地归结了我的问题之一:如何传递以 lambda 作为模板参数的模板化函子?
#include <string>
#include <iostream>
#define uint unsigned int
template <class F>
class Functor {
public:
F m_f;
template <class... Args>
decltype(auto) operator()(Args&&... args) {
return m_f(*this, std::forward<Args>(args)...);
}
};
template <class F> Functor(F)->Functor<F>;
class B {
private:
uint m_val;
public:
B(uint val) : m_val(val) {}
uint evaluate(Functor<decltype([](auto & self, uint val)->uint {})> func) const {
return func(m_val);
}
};
int main() {
B b = B(5u); …Run Code Online (Sandbox Code Playgroud) 假设我有一个包装器函数,它绕过一些较小的递归函数.但是,在调用递归函数之前,包装器会创建递归函数使用的对象.我怎么能用c ++做到这一点?我只需要把它变成自己的类吗?编辑 - 我知道如果我可以把它变成一个班级以及如何从那里开始 - 但我的问题是我需要一个班级还是我可以某种方式逃离而不制作一个?
我做了一个通用的例子来澄清我的问题:
void wrapper()
{
Object myObject;
bool recurFun(int x)
{
// do some stuff with myObject
if (some condition){return recurFun(x-1)}
else {return true}
}
}
Run Code Online (Sandbox Code Playgroud)
请忽略任何基本的语法类型错误,它不是一个工作示例,只是一个帮助我的问题传达给你们.谢谢!
为什么会导致分段错误?
#include <iostream>
#include <functional>
using namespace std;
int main() {
function<int(int,int)> hcf = [=](int m, int n)mutable->int{
// cout << "m: " << m << " n: " << n << endl;
if(m<n) return hcf(n,m);
int remainder(m%n);
if(0 == remainder) return n;
return hcf(n,remainder);
};
cout << hcf(10,15) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我添加了mutable关键字.不应该按值传递工作吗?为什么这需要传递引用?是因为我hcf递归地打电话?
我正在尝试编译它:
#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
bool isInterleave(string &s1, string &s2, string &s3) {
auto dfs = [&](int i, int j, int k) {
if (k > s3.length()) {
return true;
}
if (s1[i] == s3[k]) {
auto res = dfs(i + 1, j, k + 1);
}
};
dfs(0, 0);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
x86-64 gcc 9.3
-pedantic -Wall -O2 -std=c++2a
Could not execute the program
Compiler …Run Code Online (Sandbox Code Playgroud)