gpt4 book ai didi

c++ - 从 lambda 中调用基本成员函数时出错

转载 作者:太空狗 更新时间:2023-10-29 21:22:50 25 4
gpt4 key购买 nike

我试图在 lambda 中调用成员函数的基类实现。

clang++ 接受而 gcc 拒绝的以下代码是否符合标准?

#include <iostream>

class Base
{
protected:
void fn() { }
};

class Derived
: public Base
{
public:
void fn()
{
[&]() {
Base::fn(); /// compilation error here
}();
}
};

int main()
{
Derived d;
d.fn();
}

错误信息:

test.cpp: In lambda function:
test.cpp:6:10: error: void Base::fn() is protected
void fn() { }
^
test.cpp:16:22: error: within this context
Base::fn();
^

编译器版本:

gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8)

Ubuntu clang version 3.3-5ubuntu4 (branches/release_33) (based on LLVM 3.3)

最佳答案

这是 gcc 中的错误. lambda 就像定义本地类一样,具有相同的访问权限。

关于c++ - 从 lambda 中调用基本成员函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com