#include <folly/Malloc.h>
#include <folly/Portability.h>
#include <folly/SmallLocks.h>
+#include <folly/Traits.h>
#include <folly/portability/BitsFunctexcept.h>
#include <folly/portability/Constexpr.h>
#include <folly/portability/Malloc.h>
constructImpl(il.begin(), il.end(), std::false_type());
}
- explicit small_vector(size_type n, value_type const& t = value_type()) {
- doConstruct(n, t);
+ explicit small_vector(size_type n) {
+ doConstruct(n, [&](void* p) { new (p) value_type(); });
+ }
+
+ small_vector(size_type n, value_type const& t) {
+ doConstruct(n, [&](void* p) { new (p) value_type(t); });
}
template<class Arg>
}
}
- void doConstruct(size_type n, value_type const& val) {
+ template <typename InitFunc>
+ void doConstruct(size_type n, InitFunc&& func) {
makeSize(n);
this->setSize(n);
try {
- detail::populateMemForward(data(), n,
- [&] (void* p) { new (p) value_type(val); }
- );
+ detail::populateMemForward(data(), n, std::forward<InitFunc>(func));
} catch (...) {
if (this->isExtern()) {
u.freeHeap();
// The true_type means we should forward to the size_t,value_type
// overload.
void constructImpl(size_type n, value_type const& val, std::true_type) {
- doConstruct(n, val);
+ doConstruct(n, [&](void* p) { new (p) value_type(val); });
}
void makeSize(size_type size, value_type* v = nullptr) {
ASSERT_EQ(smallV[i], expected[i]);
}
}
+
+TEST(small_vector, NoCopyCtor) {
+ struct Test {
+ Test() = default;
+ Test(const Test&) = delete;
+ Test(Test&&) = default;
+
+ int field = 42;
+ };
+
+ small_vector<Test> test(10);
+ ASSERT_EQ(test.size(), 10);
+ for (const auto& element : test) {
+ EXPECT_EQ(element.field, 42);
+ }
+}
+
+TEST(small_vector, ZeroInitializable) {
+ small_vector<int> test(10);
+ ASSERT_EQ(test.size(), 10);
+ for (const auto& element : test) {
+ EXPECT_EQ(element, 0);
+ }
+}