简体   繁体   English

为什么以下代码编译失败(C++ lambda问题)

[英]Why does the following code failed to compile (C++ lambda problem)

Well I'm coding a function in which I used recursive lambda in C++ (GNU C++14).好吧,我正在编写 function,其中我在 C++(GNU C++14)中使用了递归 lambda。 So I tried the y_combinator method, beginning with a declaration outside the main() function (I've used using namespace std; before this).所以我尝试了y_combinator方法,从main() function 之外的声明开始(在此之前我使用过using namespace std; )。

#include <bits/stdc++.h>

using namespace std;

template<class Fun>
class y_combinator_result {
    Fun fun_;
public:
    template<class T>
    explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)) {}

    template<class ...Args>
    decltype(auto) operator()(Args &&...args) {
        return fun_(ref(*this), forward<Args>(args)...);
    }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
    return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}

int main () {
    cin.tie(0)->sync_with_stdio(0);

    int N; cin >> N;
    vector<int> adj[N+1];
    for (int i = 0; i < N-1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    vector<int> d(N+1);
    auto dfs = y_combinator([&](auto dfs, int u, int p, int &r) {
        if (u == p) d[u] = 0;
        if (d[u] > d[r]) r = u;
        for (auto &v : adj[u]) {
            if (v != p) {
                d[v] = d[u]+1;
                dfs(v, u, r);
            }
        }
    });
}

Now I have a simple, easy to understand question for you guys: What make this lambda function failed to even compile (this happen inside the main function)?现在我有一个简单易懂的问题给你们:是什么让这个 lambda function 甚至无法编译(这发生在主函数内部)?

The thing is, when I'm trying to compile it, I received this catastrophic message:问题是,当我试图编译它时,我收到了这个灾难性的消息:

test1.cpp: In function 'int main()':
test1.cpp:36:29: error: use of deleted function 'main()::<lambda(auto:1, int, int, int&)>::~<lambda>()'
   36 |     auto dfs = y_combinator([&](auto dfs, int u, int p, int &r) {
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   37 |         if (u == p) d[u] = 0;
      |         ~~~~~~~~~~~~~~~~~~~~~
   38 |         if (d[u] > d[r]) r = u;
      |         ~~~~~~~~~~~~~~~~~~~~~~~
   39 |         for (auto &v : adj[u]) {
      |         ~~~~~~~~~~~~~~~~~~~~~~~~
   40 |             if (v != p) {
      |             ~~~~~~~~~~~~~
   41 |                 d[v] = d[u]+1;
      |                 ~~~~~~~~~~~~~~
   42 |                 dfs(v, u, r);
      |                 ~~~~~~~~~~~~~
   43 |             }
      |             ~
   44 |         }
      |         ~
   45 |     });
      |     ~
test1.cpp:36:31: note: 'main()::<lambda(auto:1, int, int, int&)>::~<lambda>()' is implicitly deleted because the default definition would be ill-formed:
   36 |     auto dfs = y_combinator([&](auto dfs, int u, int p, int &r) {
      |                               ^

So I try a small fix by capturing 2 vector name d and adj所以我通过捕获 2 个向量名称dadj来尝试一个小修复

#include <bits/stdc++.h>

using namespace std;

template<class Fun>
class y_combinator_result {
    Fun fun_;
public:
    template<class T>
    explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)) {}

    template<class ...Args>
    decltype(auto) operator()(Args &&...args) {
        return fun_(ref(*this), forward<Args>(args)...);
    }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
    return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}

int main () {
    cin.tie(0)->sync_with_stdio(0);

    int N; cin >> N;
    vector<int> adj[N+1];
    for (int i = 0; i < N-1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    vector<int> d(N+1);
    auto dfs = y_combinator([&d, &adj](auto dfs, int u, int p, int &r) { // I changed this line
        if (u == p) d[u] = 0;
        if (d[u] > d[r]) r = u;
        for (auto &v : adj[u]) {
            if (v != p) {
                d[v] = d[u]+1;
                dfs(v, u, r);
            }
        }
    });
}

and the program compiled as intended... Obviously there is a very big question from my perspective: Why did the first piece of code that I used [&] to capture everything in side the main() function failed, but the second one work?并且程序按预期编译...显然从我的角度来看有一个非常大的问题:为什么我使用[&]捕获main() function 中的所有内容的第一段代码失败了,但第二段代码有效? Is there any way around this, cuz I don't want to spend time capturing variables...有什么办法可以解决这个问题,因为我不想花时间捕捉变量......

P/s: forgive me for my bad english. P/s:原谅我的英语不好。 I'm not a native speaker.我不是母语人士。

The C++ does not have VLA vector<int> adj[N+1]; C++ 没有 VLA vector<int> adj[N+1]; . . Using this VLA causes the compiler error.使用此 VLA 会导致编译器错误。 See Variable Length Array (VLA) in C++ compilers .请参阅C++ 编译器中的可变长度数组 (VLA)

Making vector<int> adj[N+1];制作vector<int> adj[N+1]; -> vector<vector<int>> adj(N+1); -> vector<vector<int>> adj(N+1); makes the example compliable.使示例兼容。

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

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