为什么 GCC 不允许我通过引用捕获某些东西,而 Clang 允许它?

问题描述

这是针对 C++20 (-std=c++20)、GCC 10.2.0 和 Clang 11.1.0。

我有一个带有数据成员 Graph 的类 prefix_to_presufs。在我的代码中,我将 std::unique_ptr 生成一个 Graph 中,如下所示:

auto gr{std::make_unique<Graph>()};

然后我尝试这样做:

auto less{[&p2p{gr->prefix_to_presufs}](PreSuf l,PreSuf r) -> bool {
        return p2p[l.suf()].size() < p2p[r.suf()].size();
}};

但是,GCC 给了我这个错误

filename.cc: In function ‘int main()’:
filename.cc:236:39: error: cannot capture ‘{gr.std::unique_ptr<{anonymous}::Graph,std::default_delete<{anonymous}::Graph> >::operator->()->{anonymous}::Graph::prefix_to_presufs}’ by reference
  236 |  auto less{[&p2p{gr->prefix_to_presufs}](PreSuf l,PreSuf r) -> bool {
      |                                       ^

我想知道我正在尝试做的事情是否不符合标准并且 Clang 是不必要的宽容,或者这是否只是一个 GCC 错误

为了回应下面的评论,如果我尝试像这样初始化:&p2p = gr->prefix_to_presufs,我的代码会编译。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)