C++ g+上的错误+;4.8.2 at列表方法参数默认初始化
我正在尝试c++11的新功能,发现了一个问题。这是我的代码:C++ g+上的错误+;4.8.2 at列表方法参数默认初始化,c++,list,c++11,initialization,C++,List,C++11,Initialization,我正在尝试c++11的新功能,发现了一个问题。这是我的代码: #include <iostream> #include <list> #include <string> using namespace std; class A { public: int f (list<string> a, list<string> b={}) { cout << a.size()
#include <iostream>
#include <list>
#include <string>
using namespace std;
class A {
public:
int f (list<string> a, list<string> b={})
{
cout << a.size() << endl;
cout << b.size() << endl; // This line!!!
return 0;
}
};
int main ()
{
A a;
list<string> l{"hello","world"};
a.f(l);
return 0;
}
我以这种方式编译我的程序:
g++ -std=c++11 -ggdb3 -fPIC -o test TestlistInit.cpp
我正在使用这个版本的g++:
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
提前感谢 要找到原因,请启用调试符号,当您到达第一行时,我们首先检查b的内容,看起来像这样(值将不同),在本例中,我使用了Code::Blocks“Watch”选项 然后使用调试器选项在我们到达b.size行时“进入” 最后我们将看到stl_迭代器_base_funcs.h 开始时,我们可以看到第一个和最后一个是相同的:
__first._M_node = 0x7fffffffe370
__last._M_node = 0x7fffffffe370
while (__first != __last)
{
++__first;
++__n;
}
首先进入++\u
我们可以在stl\u列表中看到它是这样做的。h:
_Self&
operator++()
{
_M_node = _M_node->_M_next;
return *this;
}
\u M_节点
和\u节点->\u M_下一个
是相同的,因此\u第一个
从不递增,而.size()
陷入了无休止的循环。我在4.7.3上遇到了同样的问题,有趣的是,只是替换列表b=list()
就行了。但是,是的,似乎是一个gcc错误。绝对是一个错误。当您从函数返回一个作为默认参数的参数时,您会得到一个。看看发生了什么难以置信的事情,这些基本的bug仍然存在于4.8.2中…这似乎在g++4.9.1(Debian上)上工作
__first._M_node = 0x7fffffffe370
__last._M_node = 0x7fffffffe370
while (__first != __last)
{
++__first;
++__n;
}
_Self&
operator++()
{
_M_node = _M_node->_M_next;
return *this;
}