问题
I need a container of elements that are neither copyable nor movable. These elements are not default constructible, but their constructors get identical arguments.
The size of the container does not change during it's lifetime. It should be as simple as a built-in array, but it's size is determined at run-time when the constructor is called.
Is there an easy way to implement that without the overhead of memory allocation and indirection incurred by using std::vector<std::unique_ptr<T>>
?
回答1:
Here's a simple, yet incomplete solution under the assumption that each element is constructed with the same arguments. It uses placement new to construct the elements in-place (see also this SO question):
#include <cstdlib>
#include <utility>
#include <new>
// sample structure, non-copyable, non-moveable, non-default-constructible
struct Foo
{
Foo() = delete;
Foo(const Foo&) = delete;
Foo& operator = (const Foo&) = delete;
Foo(Foo&&) = delete;
Foo& operator = (Foo&&) = delete;
Foo(int a, char b, double c) : m_a(a), m_b(b), m_c(c) { }
int m_a;
char m_b;
double m_c;
};
template <typename T>
struct MyArray
{
// Array ctor constructs all elements in-place using the
// provided parameters
template <typename... Args>
MyArray(std::size_t sz, Args&&... args)
: m_sz(sz),
m_data(static_cast<T*>(malloc(sz * sizeof(T))))
{
for (std::size_t i=0; i<m_sz; ++i)
{
new (&m_data[i]) T(std::forward<Args>(args)...);
}
}
~MyArray()
{
for (std::size_t i=0; i<m_sz; ++i)
{
m_data[i].~T();
}
free(m_data);
}
std::size_t m_sz;
T *m_data;
};
int main()
{
Foo foo(1, '2', 3.0);
std::size_t s = 5;
MyArray<Foo> foo_arr(s, 1, '2', 3.0);
}
Note that a few things are missing:
- This basic implementation will leak memory if an exception is thrown inside
MyArray
's constructor. - You will probably want an iterator implementation,
begin()
/end()
operators etc., for more convenience and to get the same behaviour as provided by the standard containers. - For illustration's sake I also didn't bother with proper encapsulation. You should probably make
m_sz
andm_data
private members.
回答2:
As the elements you want to manage are neither moveable, nor copyable, the container can only contain pointers to elements. Without knowing more or your requirement, it is hard to guess whether raw pointers or std::unique_ptr
would be more appropriate.
For a fixed-size container, a std::array
could be appropriate. Unfortunately, the size must be a compile-time expression.
An alternative would be to use a raw array, and use placement new to build elements in place. You could try to use a vector instead of a raw array, since the vector uses contiguous memory like a plain array, but I cannot imagine how you can use it to store non copyable, non moveable and non default constructible objects.
By the way, building an array on non default constructible objects with size not known at compile time in C++ in not that trivial. The only way I could find is to build a char array of proper size, declare a pointer to the class pointing to the start of the array and then build the elements with placement new:
char buf = new char[n * sizeof(X)];
X* x = reinterpret_cast<X*>(buf);
for(int i=-1; i<n i++) {
new (x + i) X(i); // or whatever appropriate ctor...
}
回答3:
"Is there an easy way to implement that without the overhead of memory allocation and indirection incurred by using std::vector>?"
The overhead is so minimal, why do you care? This is almost certainly premature optimisation, and you're just going to make yourself a maintenance headache.
来源:https://stackoverflow.com/questions/39770824/c-container-with-non-copyable-non-movable-element-type