构造“数组”不可复制对象的数量

发布于 2024-11-07 15:06:30 字数 611 浏览 0 评论 0原文

我有一个本质上不可复制的类(一个线程,因此没有有意义的复制语义),并且我想要一个较大的“数组”,用非默认构造函数相同地构造。请注意,数组的大小是固定的。

我只能对 C++ 数组使用默认构造函数,除非我独立初始化每个数组。

Thread myArray[128];   // uses default constructor - wrong

我可以显式列出对象构造函数和参数,但这又冗长又难看

Thread myArray[128] = { Thread(params,...), Thread(params,...), ... x 128 ;  // ugly

似乎我无法使用 stl 向量,因为该对象是不可复制的 - 尽管向量永远不会改变大小。我猜构造函数实际上是在进行复制!

std::vector<Thread> myVector(128, Thread(params,...));// won't compile

我这样做的方法是使用智能指针数组和初始化循环,但也许我错过了一些东西:

是否还有其他方法 - 也许使用 boost 容器或不同的容器类型?

I have a class that's inhenerently non-copyable (a thread, so there's no copy semantics that make sense), and I want to have a largeish 'array' of these, identically constructed with a non-default constructor. Note that the array is fixed size.

I can only use the default constructor with C++ arrays, unless I initialise each one independently.

Thread myArray[128];   // uses default constructor - wrong

I can list the object constructors and parameters explicitly, but that's verbose and ugly

Thread myArray[128] = { Thread(params,...), Thread(params,...), ... x 128 ;  // ugly

It seems I can't use stl vectors because the object is non-copyable - event though the vector never changes size. I guess the constructor actually does copying!

std::vector<Thread> myVector(128, Thread(params,...));// won't compile

The way I've though of doing this is with an array of smart pointers and an initialization loop, but maybe I'm missing something:

Is there any other way - maybe with boost containers, or a different container type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

别闹i 2024-11-14 15:06:30

这可能看起来完全疯狂(而且可能确实如此),但是......

struct ThreadInitValues{
  // your actual params
  int i;
  float f;
};

struct Thread{
    Thread(int i = _init.i, float f = _init.f)
      : _i(i)
      , _f(f)
    {}

    static ThreadInitValues _init;

private:
    // uncopyable
    Thread(Thread const&);
    Thread& operator=(Thread const& other);

    // your actual member
    int _i;
    float _f;
};

ThreadInitValues Thread::_init;

int main(){
    Thread::_init.i = 5;
    Thread::_init.f = 3.14f;
    Thread arr[128];
}

也许这对你有用。 :) 当然,您现在需要注意数组是否是在多线程代码本身中初始化的......

This may seem like totally crazy (and it probably is), but...

struct ThreadInitValues{
  // your actual params
  int i;
  float f;
};

struct Thread{
    Thread(int i = _init.i, float f = _init.f)
      : _i(i)
      , _f(f)
    {}

    static ThreadInitValues _init;

private:
    // uncopyable
    Thread(Thread const&);
    Thread& operator=(Thread const& other);

    // your actual member
    int _i;
    float _f;
};

ThreadInitValues Thread::_init;

int main(){
    Thread::_init.i = 5;
    Thread::_init.f = 3.14f;
    Thread arr[128];
}

Maybe this works out for you. :) Of course, you now need to watch out if the array is initialized in multithreaded code itself...

峩卟喜欢 2024-11-14 15:06:30

智能指针向量,每个实例都是动态的
分配,绝对是最简单的方法。否则(我会
仅在绝对必要时才这样做),您或多或少可以
模拟 std::vector 内部的作用。沿途有什么东西
行:(

union
{
    double just_to_ensure_alignment;
    unsigned char data[ sizeof(Thread) * elementCount ];
} array;

//  construct...
for ( int i = 0; i != elementCount; ++ i )
    new (data + i * sizeof(Thread)) Thread(params,...);

//  access...
Thread& getAt( int i )
{
    return reinterpret_cast<Thread*>( data + i * sizeof(Thread) );
}

我实际上会将其包装在一个类中,如果只是为了能够使用
operator[] 而不是 getAt。)

A vector of smart pointers, with each instance dynamically
allocated, is definitely the simplest way. Otherwise (and I'd
only do this if absolutely necessary), you can more or less
emulate what std::vector does internally. Something along the
lines of:

union
{
    double just_to_ensure_alignment;
    unsigned char data[ sizeof(Thread) * elementCount ];
} array;

//  construct...
for ( int i = 0; i != elementCount; ++ i )
    new (data + i * sizeof(Thread)) Thread(params,...);

//  access...
Thread& getAt( int i )
{
    return reinterpret_cast<Thread*>( data + i * sizeof(Thread) );
}

(I'd actually wrap this in a class, if only to be able to use
operator[] instead of getAt.)

无需解释 2024-11-14 15:06:30

对于支持右值引用的新编译器,据我所知,向量元素不需要可复制。要在不复制的情况下使用它,应该使用 128 个 push_back(每次创建新对象),因为从一个对象创建多个对象是一种复制:)。

如果这种方式不可用,请尝试 boost::ptr_vector< /a> 或 boost::shared_ptr 的 std::vector。

For new compilers supporting r-value references, being copyable is not necessary for vector elements AFAIK. To use it without copying 128 push_back's should be used (creating new object each time), because creting several objects from a single one is a copying :).

If this way is not available, try boost::ptr_vector or std::vector of boost::shared_ptr.

童话里做英雄 2024-11-14 15:06:30

我不是那么大的程序员。但你尝试过这个吗

std::vector<YourObject *> temp_Vec;
for(int i=0;i<128;++i)
    temp_Vec.push_back(new YourObject(arguments));

I am not that much big programmer. But did you try this

std::vector<YourObject *> temp_Vec;
for(int i=0;i<128;++i)
    temp_Vec.push_back(new YourObject(arguments));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文