c++ - 为什么 lambda 上的朋友声明在 gcc 上不起作用
问题描述
看这个简单的例子,
constexpr auto f = [](const auto& foo){
foo.fun2();
};
struct Foo{
friend decltype(f);
void fun1() const{
f(*this);
}
private:
void fun2() const{}
};
int main(){
Foo foo{};
foo.fun1();
}
朋友声明似乎不起作用。gcc 抱怨它fun2
是私有的,即使f
被授予访问权限。
这会导致编译器错误
<source>: In instantiation of '<lambda(const auto:1&)> [with auto:1 = Foo]':
<source>:9:16: required from here
<source>:2:13: error: 'void Foo::fun2() const' is private within this context
2 | foo.fun2();
| ~~~~~~~~^~
<source>:13:10: note: declared private here
13 | void fun2() const{}
解决方案
推荐阅读
- c++ - 在并行代码中调试分段错误
- android-emulator - 如何通过 Bluestacks 上的“adb 备份/恢复”确认解锁?
- shiny - 在选择 Advanced checkboxInput 时禁用/启用 selectInput 和 fileInput
- python - Python selenium 给了我一个空的文本字符串
- ruby-on-rails - Rails 6 API 仅不保存嵌套属性
- css - CSS 和外部参考样式表页面
- python - 抓取表,其中行需要与先前的元素相关联
- excel - 将单元格值与 VBA 组合
- ckeditor4.x - 尝试在配置中加载多个 pugin,但只加载最后一个
- c# - 无法将值从一个脚本传递到另一个