C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – gcc无法使用此捕获编译通用lambda大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > 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月尚未解决.

大佬总结

以上是大佬教程为你收集整理的c – gcc无法使用此捕获编译通用lambda全部内容,希望文章能够帮你解决c – gcc无法使用此捕获编译通用lambda所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。