加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – gcc无法使用此捕获编译通用lambda

发布时间:2020-12-16 03:00:38 所属栏目:百科 来源:网络整理
导读:参见英文答案 Calling `this` member function from generic lambda – clang vs gcc1 我无法使用gcc 6.1编译以下程序: #include iostream#include string#include vector#include iterator#include algorithmclass Foo{public: void apply() const { std::
参见英文答案 > Calling `this` member function from generic lambda – clang vs gcc1
我无法使用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))进行编译.

这是编译器的错误吗?

解决方法

这是一份记录在案的 gcc bug,截至2016年8月尚未解决.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读