如果我有代码:
struct Test
{
int x = 10;
};
int main()
{
std::list<Test> linkedList;
std::cout << linkedList.front().x << std::endl;
}
---
out -> 0
为什么我的 test.x 值是 0?如果我将列表更改为 int 类型,它会返回 0。如果我给它一个 char 类型,我什么也得不到(或“”)。
我很好奇它是如何(以及为什么)在幕后发生的。它如何处理返回任何类型的值而不退出程序或需要 try/catch?
最佳答案
linkedList
是一个空的列表
。调用front()
其结果是UB ,意味着一切皆有可能。
Returns a reference to the first element in the container.
Calling front on an empty container is undefined.
关于c++ - c++ std::list::front() 返回一个空列表是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943480/