参见英文答案 >
Calling `this` member function from generic lambda – clang vs gcc1
我无法使用gcc 6.1编译以下程序:
我无法使用gcc 6.1编译以下程序:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <algorithm> class Foo { public: void apply() const { std::for_each(std::cbegin(bars_),std::cend(bars_),[this] (const auto& x) { print(x); }); } private: std::vector<std::string> bars_; void print(const std::string& x) const { std::cout << x << ' '; } }; int main() { Foo foo {}; foo.apply(); return 0; }
错误信息是:
error: cannot call member function 'void Foo::print(const string&) const' without object std::for_each(std::cbegin(bars_),[this] (const auto& x) { print(x); }); ^~~~~
>更改const auto& x到const std :: string& x使程序编译.
>将print(x)更改为 – > print(x)使程序编译.
>所有版本都使用Clang(Apple LLVM 7.3.0版(clang-703.0.31))进行编译.
这是编译器的错误吗?