我有一个奇怪的问题,在尝试创建一个小例子在stackoverflow上发布时,我无法重现 . 希望这仍然会给某人敲响钟声,或者有人有进一步挖掘的好主意......
在我的Mac上,下面的代码使用gcc 4.9.2进行编译 . 我正在使用 g++ -std=c++11 test.cpp
. 在其他一些使用gcc 4.7.2的Linux / Fedora机器上,我收到编译错误 . 不是在下面的测试示例,而是在一个更复杂的问题上 . 但是,我不允许在这里发帖,也无法看到究竟有什么不同 .
但是,通过简单地尝试很多东西,我确实找到了一种编译方法 . 我希望有人可能会看到该提示有什么问题 . 我可以让程序编译的方法是将lambda体从 v.push_back...
更改为 this->v.push_back...
知道为什么会这样吗?
我看到的编译错误是:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c /4.7.2/bits/stl_vector.h:881:7:注意: void std :: vector <_Tp,_Alloc> :: push_back(const value_type&)[with _Tp = aggregate; _Alloc = std :: allocator <aggregate>; std :: vector <_Tp,_Alloc> :: value_type = aggregate] <near match> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/ c /4.7.2/bits/stl_vector.h:881:7:注意:从'const std :: vector <aggregate> *'到'std :: vector <aggregate> *'的隐式'this'参数没有已知的转换/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c /4.7.2/bits/stl_vector.h:899:7:注意:无效std :: vector <_Tp,_Alloc> :: push_back(std :: vector <_Tp,_Alloc> :: value_type &&)[with _Tp = aggregate; _Alloc = std :: allocator <aggregate>; std :: vector <_Tp,_Alloc> :: value_type = aggregate] <near match> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/ c /4.7.2/bits/stl_vector.h:899:7:注意:参数1从'aggregate'到'std :: vector <aggregate> :: value_type && {aka aggregate &&}'没有已知的转换
以下无法使用gcc 4.7.2在Linux机器上进行编译:
#include <vector>
#include <iostream>
#include <functional>
struct aggregate {
int foo;
char bar[2];
};
template<typename T>
class test {
private:
std::vector<aggregate> v;
std::function<void(aggregate&)> lambda;
public:
test() :
lambda([this] (aggregate& a) { v.push_back(a); })
{
v.reserve(8);
}
void execute() {
aggregate a{1, "x"};
lambda(a);
}
};
int main() {
test<int> t;
t.execute();
}
$ g -std = c 11 test.cpp test.cpp在lambda函数中:test.cpp:17:53:错误:没有用于调用'std :: vector <aggregate> :: push_back(aggregate&)const'的匹配函数test.cpp:17:53:注意:候选人是:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c /中包含的文件4.7.2 / vector:65:0,来自test.cpp:1:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c / 4.7.2 / bits / stl_vector.h:881:7:注意:void std :: vector <_Tp,_Alloc> :: push_back(const value_type&)[with _Tp = aggregate; _Alloc = std :: allocator <aggregate>; std :: vector <_Tp,_Alloc> :: value_type = aggregate] <near match> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/ c /4.7.2/bits/stl_vector.h:881:7:注意:从'const std :: vector <aggregate> *'到'std :: vector <aggregate> *'的隐式'this'参数没有已知的转换/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c /4.7.2/bits/stl_vector.h:899:7:注意:无效std :: vector <_Tp,_Alloc> :: push_back(std :: vector <_Tp,_Alloc> :: value_type &&)[with _Tp = aggregate; _Alloc = std :: allocator <aggregate>; std :: vector <_Tp,_Alloc> :: value_type = aggregate] <near match> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/ c /4.7.2/bits/stl_vector.h:899:7:注意:参数1从'aggregate'到'std :: vector <aggregate> :: value_type && {aka aggregate &&}'没有已知的转换
但在将 v.push_back...
改为 this->v.push_back...
后工作正常
2 回答
上面可能是GCC 4.7.2中这个bug的表现吗?
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54277
这个答案是在评论主题中得出的,并且正在这里作为可能的答案进行考虑 . 如果不出意外,它是我发现的最接近的解释正在发生的事情以及为什么在新的编译器中不会发生这种情况 .
我'll bet it'是对
aggregate
的RValue引用 . 请注意,它的类型为aggregate&&
而不是aggregate&
,并且在gcc 4.7中不受支持 .https://gcc.gnu.org/gcc-4.7/cxx0x_status.html