在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);
编译器仍然看不到我真正指的静态函数。有人有任何出色的编译器标记可能会对此或其他建议进行调整吗?谢谢