小编lac*_*chy的帖子

Idiomatic way to create an immutable and efficient class in C++

I am looking to do something like this (C#).

public final class ImmutableClass {
    public readonly int i;
    public readonly OtherImmutableClass o;
    public readonly ReadOnlyCollection<OtherImmutableClass> r;

    public ImmutableClass(int i, OtherImmutableClass o,
        ReadOnlyCollection<OtherImmutableClass> r) : i(i), o(o), r(r) {}
}
Run Code Online (Sandbox Code Playgroud)

The potential solutions and their associated problems I've encountered are:

1. Using const for the class members, but this means the default copy assignment operator is deleted.

Solution 1:

struct OtherImmutableObject {
    const int i1;
    const int i2;

    OtherImmutableObject(int i1, …
Run Code Online (Sandbox Code Playgroud)

c++ const immutability const-cast

37
推荐指数
4
解决办法
2920
查看次数

std::scoped_lock 或 std::unique_lock 或 std::lock_guard ?

这个问题我明白这std::scoped_lock是“一个严格的高级版本std::lock_guard”。

这个问题我明白“std::lock_guardstd::unique_lock是一样的”,除了std::unique_lock有一些额外的功能(例如try_lock),但需要一些额外的开销。

std::scoped_lock相比如何std::unique_lock

我希望通过这个问题回答一些相关的问题。

  1. std::scoped_lock和之间有什么区别std::unique_lock
  2. 在什么情况下应该使用std::scoped_lock而不是std::unique_lock
  3. 在什么情况下应该使用std::unique_lock而不是std::scoped_lock
  4. 为什么不std::scoped_lock实现 的一些附加功能std::unique_lock

c++ multithreading locking c++11 c++17

22
推荐指数
1
解决办法
6156
查看次数

在Python的Multiprocessing库中获取Manager.Queue的长度

我有一个队列,其中包含我希望一组进程执行的所有工作.我想获得此队列中剩余的元素数量,并想知道如何做到这一点?该LEN功能似乎并没有工作,虽然我可以重复的队列中获取的每个元素,并把它放回,直到我走了一圈,我宁愿避免这种情况的畏缩编码的原因.

python python-2.7 python-multiprocessing

2
推荐指数
1
解决办法
6272
查看次数

编写一个"记住"传入其中的值的Lambda函数

我只是想知道如何在C++中编写一个Lambda函数,它"记住"下次调用它时传入的值?具体来说,我正在考虑i=iPython 的语法如下:

funs = [(lambda i=i: i) for i in range(10)]
Run Code Online (Sandbox Code Playgroud)

如果运行以下代码:

for i in range(len(funs)):
    print funs[i]()
Run Code Online (Sandbox Code Playgroud)

结果是:

0
1
4
9
16
Run Code Online (Sandbox Code Playgroud)

我也想知道技术名称(如果存在的话)是什么?(我知道,如果我知道第二个问题的答案,我可以通过Google获得解决方案......)

c++ c++11

1
推荐指数
1
解决办法
95
查看次数

我可以用 C# 创建只读索引器吗?

这个 SO 问题中,我们了解如何为类创建索引器。是否可以为类创建只读索引器?

下面是微软提供的 Indexer 示例:

using System;

class SampleCollection<T>
{
   // Declare an array to store the data elements.
   private T[] arr = new T[100];

   // Define the indexer to allow client code to use [] notation.
   public T this[int i]
   {
      get { return arr[i]; }
      set { arr[i] = value; }
   }
}

class Program
{
   static void Main()
   {
      var stringCollection = new SampleCollection<string>();
      stringCollection[0] = "Hello, World";
      Console.WriteLine(stringCollection[0]);
   }
}
// The example …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
446
查看次数