【发布时间】:2018-07-09 07:47:49
【问题描述】:
在以下代码中,从 g++4.1 到 4.9 的编译失败。 “错误”似乎已从 g++-5.1 得到纠正。我在 gcc.godbolt.org 下尝试了 -std=c++11 编译标志。
#include <algorithm>
#include <vector>
class A
{
protected:
struct S
{
int a;
int b;
};
};
class B : public A
{
std::vector<A::S> v;
public:
void foo(std::vector<A::S> v)
{
std::sort(v.begin(), v.end(),
[](const A::S& a, // <--- error only here !
const A::S& b) { return a.a < b.b; });
}
void auto_sort()
{
foo(this->v);
}
};
int main()
{
B b;
b.auto_sort();
return 1;
}
返回的错误是:'struct A::S' is protected 它只出现在 lambda 函数声明中。
我的问题是:这是 g++-4.X 中的错误吗?因此可以纠正吗?或者它是自 g++-5.1 以来改变的 c++ 规则?还是我错过了什么?
我是否必须将 lambda 函数编写为 B 作为参数传递的方法?
当然,我的目标是在任何 g++ 版本下编译代码。
谢谢,
【问题讨论】:
-
g++4 在 c++11 方面存在缺陷。用这个编译器不支持 c++11 可能是明智的,没有人会使用这个编译器。
-
天哪,已经有这么多个版本了...当前版本是8.1!