gpt4 book ai didi

c++ - 模板类型是否浪费 C++ 中的空间?

转载 作者:可可西里 更新时间:2023-11-01 16:05:48 25 4
gpt4 key购买 nike

我正在浏览 EASTL's list class查看作者如何实现节点。我的期望是一个简单的类/结构。相反,我看到一个基类和一个从该基类继承的节点(仍然很简单,但为什么是两个类?)。他的评论解释了原因:

We define a ListNodeBase separately from ListNode (below), because it allows us to have non-templated operations such as insert, remove (below), and it makes it so that the list anchor node doesn't carry a T with it, which would waste space and possibly lead to surprising the user due to extra Ts existing that the user didn't explicitly create. The downside to all of this is that it makes debug viewing of a list harder, given that the node pointers are of type ListNodeBase and not ListNode. However, see ListNodeBaseProxy below.

我不明白这里的一些事情。我确实理解为什么它会使调试查看变得有点困难,但他所说的 list anchor node doesn't carry a T with it 是什么意思?和 would waste space and possibly lead to surprising the user due to extra Ts existing that the user didn't explicitly create

最佳答案

如果没有助手类,列表根节点将包含一个从未使用过的 T 实例。第二句是说你可能不希望一个空列表创建一个 T。例如,创建一个 T 可能会有副作用。

关于c++ - 模板类型是否浪费 C++ 中的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com