如何将conditional_variable 对象插入Vector? [英] How to insert a conditional_variable object into a Vector?
问题描述
conditional_variable 不是 CopyConstructible、MoveConstructible、CopyAssignable、MoveAssignable.
conditional_variable is not CopyConstructible, MoveConstructible, CopyAssignable, MoveAssignable.
我们可以这样称呼吗
vector<conditional_variable> cond;
conditional_variable c1;
conditional_variable c2;
cond.push_back(c1);
cond.push_back(c2);
在这些情况下进行的正确方法是什么
What is the right way to proceed in these scenarios
推荐答案
您可以创建一个向量,该向量可以默认构造但不能通过使用带有大小参数的构造函数进行复制或移动:
You can create a vector of something that can be default constructed but can't be copied or moved by using the constructor taking a size argument:
std::vector<std::condition_variable> cv_vec(20);
这样的向量不能增长,但可以用 pop_back()
或 clear()
缩小(但不是 erase()
或resize()
).
Such a vector cannot be grown, but may be shrunk with pop_back()
or clear()
(but not erase()
or resize()
).
或者,由于一切都可以通过额外的间接级别来解决,因此您可以使用 std::unique_ptr
向量代替.
Alternatively, since everything can be solved with an extra level of indirection, you can have a vector of std::unique_ptr<std::condition_variable>
instead.
现在,为什么有人想要为 condition_variable
这样的同步原语这样做,我不知道......
Now, why on earth someone would want to do this for a synchronization primitive like condition_variable
, I have no idea...
这篇关于如何将conditional_variable 对象插入Vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!