搜索有关如何在 C++ 中实现不可变数据结构的提示;
我想知道如何在 C++(或 C)中实现不可变数据结构。我正在寻找关于该主题的一本书或论文(或相对简单且有记录的实现),但目前我还没有找到,所以我决定寻求提示。 预先感谢您的回答。
I'm wondering how to implement immutable data structures in C++ (or C). I'm searching for a book or paper (or a relatively simple and documented implementation) on the subject and I haven't managed to find one for now so I decided to ask for hints.
Thanks in advance for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我想你可能会从其他语言中得到一些想法。例如,在 Java 和 C# 中,不变性的实现方式如下。我们不创建“变异器”(“变异”对象状态的函数),而是创建返回新“更改”实例的函数:
I think you may take an idea from another languages. For example in Java and C# immutability implemented following way. Instead of creating "mutators" (functions that "mutate" objects' state), we create functions that return new "changed" instance:
您可以将对象声明为 const,如下所示:
const std::string x("My Const String");
这是使用 const 关键字使对象不可变的常见方法。
如果你知道你有一个对象,你不想允许任何东西被改变,这应该是
作为该对象的每个实例的行为的一部分,您可以创建一个包含所有 const 成员的对象。
请注意,您必须在初始化列表中设置 const 成员的值。 (无论如何你都应该使用它作为最佳实践)
You can either declare objects as const like:
const std::string x("My Const String");
This is a common way of using the const keyword to make an object immutable.
If you know you have an object that you do not want to allow anything to be changed in and this should be
part of the behavior of every instance of that object, you can make an object with all const members.
Note that you must set the values of const members within an initialization list. (which you should be using anyway as best practice)
const
是您的朋友 - 具有所有数据成员const
的对象很难更改。const
is your friend - objects with all data membersconst
are hard to alter.查看 Phoenix 框架。
Phoenix 扩展了FP 的概念比 C++ 更进一步。简而言之,该框架开放了 Lambda(未命名函数)和 Currying(部分函数求值)等 FP 技术。
因此它超越了简单的不可变结构,但我认为这就是您的发展方向。
Have a look at the Phoenix Framework.
Phoenix extends the concepts of FP to C++ much further. In a nutshell, the framework opens up FP techniques such as Lambda (unnamed functions) and Currying (partial function evaluation).
So it goes beyond simple immutable structures, but I assume that is the direction you're headed.
提示:按如下方式声明一个
struct
:这会锁定赋值运算符。现在确保该结构没有
public
或mutable
成员变量,并且其所有公共方法都是const
:并且您有一些非常接近于不可变类型。当然,C++ 中缺少
sealed
/final
意味着某人无论如何都可以从您的类型派生出不太不可变的类型。Hint: Declare a
struct
as follows:This locks down the assignment operator. Now make sure the struct has no
public
ormutable
member variables and that all its public methods areconst
:And you have something very close to an immutable type. Of course, the lack of
sealed
/final
in C++ means that someone can derive a less immutable type from yours anyway.取决于“不可变数据结构”的含义。
如果您的意思是,该类型的变量不能分配给以其他方式修改的类型,那么请参阅此处的其他答案(删除对赋值运算符的访问,使用
const
,或者只是有一个引用或 < code>const 数据成员)。如果您的意思是,该类型的值无法修改,例如Java或C#或Python字符串,但仍然可以分配该类型的变量,那么这就更棘手了。您最好的帮助可能是
boost::intrusive_ptr
来管理内部可变状态。intrusive_ptr
与shared_ptr
相对的原因是intrusive_ptr
允许更多优化,其中之一对于“不可变字符串”至关重要,即,从字面量构建这样一个野兽,根本没有动态分配。我不知道有任何通用框架可以在 C++ 中执行后一种“不可变数据结构”。
所以,看来你提出了一个好主意! :-)
Depends what you mean by "immutable data structure".
If you mean, a type where variables of that type can't be assigned to otherwise modified, then see the other answers here (removing access to assignment operator, using
const
, or simply having a reference orconst
data member).If you mean, a type where values of that type can't be modified,e.g. like a Java or C# or Python string, but where variables of that type still can be assigned, then it's more tricky. Your best help there may be
boost::intrusive_ptr
to manage an internal mutable state. The reason forintrusive_ptr
as opposed to e.g.shared_ptr
is thatintrusive_ptr
allows more optimizations, one of which is crucial for e.g. "immutable strings", namely, constructing such a beast from a literal with no dynamic allocation at all.I'm not aware of any general framework for doing the latter kind of "immutable data structure" in C++.
So, it seems that you've put up a good idea! :-)
我在 Github 上发现了一个名为 gorgone 的项目,其中包含 PersistentVector(基于 Rich Hickey 在 Clojure 中的 Java 实现)和 RRBVector(基于 Phil Bagwell 最近的工作)的 C++ 实现。
该项目的维护者不再追求它,但我是。在这里查看我的叉子:https://github.com/sandover/gorgone
I found a project on Github called gorgone, which contains a C++ implementation of PersistentVector (based on Rich Hickey's Java implementation in Clojure) and RRBVector (based on more recent work by Phil Bagwell).
The maintainer of that project is no longer pursuing it, but I am. See my fork here: https://github.com/sandover/gorgone
我忍不住推荐这篇文章不可变的C++堆栈 - 想法和性能,代码审查和回答真是太棒了!
另外,要开始使用 C++ 中的
持久数据结构
或函数式数据结构
,请阅读这篇文章 C++ 中的函数式数据结构:列表。I can't help to recommend this article Immutable C++ stack - thoughts and performance, the code review and answer is really awesome!
Also, for getting started with
persistent data structures
orfunctional data structures
in C++, read this post Functional Data Structures in C++: Lists.