【发布时间】:2014-12-26 17:41:09
【问题描述】:
我正在尝试 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() << endl;
cout << b.size() << endl; // This line!!!
return 0;
}
};
int main ()
{
A a;
list<string> l{"hello","world"};
a.f(l);
return 0;
}
执行卡在“这一行!!!”线。我继续调试,看起来问题就在这里。
/** Returns the number of elements in the %list. */
size_type
size() const _GLIBCXX_NOEXCEPT
{ return std::distance(begin(), end()); }
我是这样编译我的程序的:
g++ -std=c++11 -ggdb3 -fPIC -o test TestlistInit.cpp
我正在使用这个版本的 g++:
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
提前谢谢!!!
【问题讨论】:
-
我在 4.7.3 上遇到了同样的问题,有趣的是只是替换
list<string> b = list<string>()有效。但是,是的,似乎是一个 gcc 错误。 -
绝对是一个错误。当您从函数中将它作为默认参数返回时,您会得到一个 segmentation fault。看看会发生什么when the list contains elements.
-
令人难以置信的是,4.8.2 中仍然存在这样的基本错误 ...
-
这似乎适用于 g++ 4.9.1(在 Debian 上)
标签: c++ list c++11 initialization