搜索有关如何在 C++ 中实现不可变数据结构的提示;

发布于 2024-09-27 11:36:15 字数 96 浏览 5 评论 0原文

我想知道如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

み青杉依旧 2024-10-04 11:36:15

我想你可能会从其他语言中得到一些想法。例如,在 Java 和 C# 中,不变性的实现方式如下。我们不创建“变异器”(“变异”对象状态的函数),而是创建返回新“更改”实例的函数:

class Foo
{
public:
  Foo(int i)
   : i_(i)
   {}

   int GetI() const {return i_;}
   Foo SetI(int i) const {return Foo(i);}
private:
   const int i_;
};

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:

class Foo
{
public:
  Foo(int i)
   : i_(i)
   {}

   int GetI() const {return i_;}
   Foo SetI(int i) const {return Foo(i);}
private:
   const int i_;
};
违心° 2024-10-04 11:36:15

您可以将对象声明为 const,如下所示:

const std::string x("My Const String");

这是使用 const 关键字使对象不可变的常见方法。

如果你知道你有一个对象,你不想允许任何东西被改变,这应该是
作为该对象的每个实例的行为的一部分,您可以创建一个包含所有 const 成员的对象。

class Immutable
{
   public:
   Immutable() :z(10), y(20) 
   {

   }

   Immutable(int zVal, int yVal) : z(zVal), y(yVal) 
   {

   }

   int getZ() const;
   int getY() const;

   private:
   const int z;
   const int y;
};

请注意,您必须在初始化列表中设置 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.

class Immutable
{
   public:
   Immutable() :z(10), y(20) 
   {

   }

   Immutable(int zVal, int yVal) : z(zVal), y(yVal) 
   {

   }

   int getZ() const;
   int getY() const;

   private:
   const int z;
   const int y;
};

Note that you must set the values of const members within an initialization list. (which you should be using anyway as best practice)

远昼 2024-10-04 11:36:15

const 是您的朋友 - 具有所有数据成员 const 的对象很难更改。

const is your friend - objects with all data members const are hard to alter.

£冰雨忧蓝° 2024-10-04 11:36:15

查看 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.

情愿 2024-10-04 11:36:15

提示:按如下方式声明一个struct

struct Immutable
{
private:
    Immutable& operator =(const Immutable& other);
};

这会锁定赋值运算符。现在确保该结构没有 publicmutable 成员变量,并且其所有公共方法都是 const

public:
    int get_quux() const;
    void foo(int bar) const;

并且您有一些非常接近于不可变类型。当然,C++ 中缺少 sealed/final 意味着某人无论如何都可以从您的类型派生出不太不可变的类型。

Hint: Declare a struct as follows:

struct Immutable
{
private:
    Immutable& operator =(const Immutable& other);
};

This locks down the assignment operator. Now make sure the struct has no public or mutable member variables and that all its public methods are const:

public:
    int get_quux() const;
    void foo(int bar) const;

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.

桜花祭 2024-10-04 11:36:15

取决于“不可变数据结构”的含义。

如果您的意思是,该类型的变量不能分配给以其他方式修改的类型,那么请参阅此处的其他答案(删除对赋值运算符的访问,使用 const,或者只是有一个引用或 < code>const 数据成员)。

如果您的意思是,该类型的无法修改,例如Java或C#或Python字符串,但仍然可以分配该类型的变量,那么这就更棘手了。您最好的帮助可能是 boost::intrusive_ptr 来管理内部可变状态。 intrusive_ptrshared_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 or const 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 for intrusive_ptr as opposed to e.g. shared_ptr is that intrusive_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! :-)

深居我梦 2024-10-04 11:36:15

我在 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

屋顶上的小猫咪 2024-10-04 11:36:15

我忍不住推荐这篇文章不可变的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 or functional data structures in C++, read this post Functional Data Structures in C++: Lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文