首页 > 解决方案 > Red Hat 上的 GCC 8.3.1-3 在具有使用模板参数的静态函数的模板类上失败

问题描述

在 RedHat 7 上使用 devtoolset-8 时:

[eftlab@49af022e5a7c git]$ which g++
/opt/rh/devtoolset-8/root/usr/bin/g++
[eftlab@49af022e5a7c git]$ /opt/rh/devtoolset-8/root/usr/bin/g++ --version
g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[eftlab@49af022e5a7c git]$ 

我遇到了一个我想验证的错误:

#include <iostream>
#include <functional>

template<class ParentT>
class OpenSession {
public:
  std::function<void()> Run() {
    auto parent    = parent_;

    return [parent](std::function<void()> onSuccess, std::function<void()> onReject) mutable {
      std::function<void()> toRun = [](){};

      RunCallback(parent, toRun);
    };
  }

  void RunCallback(std::function<void()>& toRun) {}

private:
  ParentT* parent_ = nullptr;

  static void RunCallback(ParentT* parent, std::function<void()>& toRun) {}
};


int main(int, const char**) {
  return 0;
}

编译它g++ -std=c++17 test.cpp,我得到以下错误:

test.cpp: In lambda function:
test.cpp:13:32: error: 'this' was not captured for this lambda function
       RunCallback(parent, toRun);
                                ^

在我看来,当 lambda 捕获正在检查其参数时,它看起来像在模板类中,它不知道这里有对模板函数的引用,这让我重命名了我的方法,因为即使更改为OpenSession<ParentT>::RunCallback(parent, toRun);编译器仍然看不到我真正指的是静态函数。有没有人有任何很棒的编译器标志可以调整这个或任何其他建议?谢谢

标签: gccg++redhat

解决方案


推荐阅读