COMMON: Allow construction of Arrays of non-copyable members

Although the previous count-constructor would never make a copy of
a member at runtime, Array<T>::reserve *may* copy-construct, so
the compiler would forbid creation of arrays of NonCopyable objects
even when the array was created only once and then never resized
(and thus never actually tried to perform a copy-construction).
This commit is contained in:
Colin Snover 2017-10-06 21:05:38 -05:00
parent 79dd02373c
commit f037d4df16
2 changed files with 8 additions and 2 deletions

View File

@ -63,9 +63,10 @@ public:
* Constructs an array with `count` default-inserted instances of T. No
* copies are made.
*/
explicit Array(size_type count) : _size(0) {
explicit Array(size_type count) : _size(count) {
allocCapacity(count);
resize(count);
for (size_type i = 0; i < count; ++i)
new ((void *)&_storage[i]) T();
}
/**

View File

@ -1,6 +1,7 @@
#include <cxxtest/TestSuite.h>
#include "common/array.h"
#include "common/noncopyable.h"
#include "common/str.h"
class ArrayTestSuite : public CxxTest::TestSuite
@ -315,6 +316,10 @@ class ArrayTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(array.size(), 10U);
TS_ASSERT_EQUALS(array[0], 0);
TS_ASSERT_EQUALS(array[9], 0);
// This will fail at compile time if it is not possible to construct an
// array without copy-construction
Common::Array<Common::NonCopyable> nonCopyable(1);
}
void test_array_constructor_count_copy_value() {