1
0
forked from boostorg/core

Add noinit_adapt() free function utility

This commit is contained in:
Glen Fernandes
2019-04-29 00:23:59 -04:00
parent 50491408b1
commit a0e1100421
3 changed files with 47 additions and 39 deletions

View File

@ -34,8 +34,7 @@ initialization of elements of a trivial type, which are later assigned values.
int main() int main()
{ {
std::vector<int, std::vector<int, boost::noinit_adaptor<std::allocator<int> > > v(5);
boost::noinit_adaptor<std::allocator<int> > > v(5);
std::iota(v.begin(), v.end(), 1); std::iota(v.begin(), v.end(), 1);
} }
``` ```
@ -136,6 +135,15 @@ const noinit_adaptor<U>& rhs) noexcept;`]
[endsect] [endsect]
[section Free functions]
[variablelist
[[`template<class A> noinit_adaptor<A> noinit_adapt(const A& a) noexcept;`]
[[variablelist
[[Returns][`noinit_adaptor<A>(a)`.]]]]]]
[endsect]
[endsect] [endsect]
[endsect] [endsect]

View File

@ -91,6 +91,13 @@ operator!=(const noinit_adaptor<T>& lhs,
return !(lhs == rhs); return !(lhs == rhs);
} }
template<class A>
inline noinit_adaptor<A>
noinit_adapt(const A& a) BOOST_NOEXCEPT
{
return noinit_adaptor<A>(a);
}
} /* boost */ } /* boost */
#endif #endif

View File

@ -19,36 +19,36 @@ public:
typedef creator<U> other; typedef creator<U> other;
}; };
creator(int* count) creator(int state)
: count_(count) { } : state_(state) { }
template<class U> template<class U>
creator(const creator<U>& other) creator(const creator<U>& other)
: count_(other.count()) { } : state_(other.state()) { }
template<class U, class V> template<class U, class V>
void construct(U*, const V&) { void construct(U*, const V&) {
++(*count_); BOOST_ERROR("construct");
} }
template<class U> template<class U>
void destroy(U*) { void destroy(U*) {
--(*count_); BOOST_ERROR("destroy");
} }
int* count() const { int state() const {
return count_; return state_;
} }
private: private:
int* count_; int state_;
}; };
template<class T, class U> template<class T, class U>
inline bool inline bool
operator==(const creator<T>& lhs, const creator<U>& rhs) operator==(const creator<T>& lhs, const creator<U>& rhs)
{ {
return lhs.count() == rhs.count(); return lhs.state() == rhs.state();
} }
template<class T, class U> template<class T, class U>
@ -73,35 +73,28 @@ private:
int value_; int value_;
}; };
inline bool
operator==(const type& lhs, const type& rhs)
{
return lhs.value() == rhs.value();
}
template<class A>
void test(const A& allocator)
{
std::vector<int, A> v(allocator);
v.push_back(1);
BOOST_TEST(v.front() == 1);
v.clear();
v.resize(5);
v.front() = 1;
}
int main() int main()
{ {
{ test(boost::noinit_adaptor<creator<int> >(1));
int c = 0; test(boost::noinit_adaptor<creator<type> >(2));
std::vector<int, boost::noinit_adaptor<creator<int> > > v(&c); test(boost::noinit_adapt(creator<int>(3)));
BOOST_TEST(c == 0); test(boost::noinit_adapt(creator<type>(4)));
v.push_back(1);
BOOST_TEST(v.front() == 1);
v.push_back(2);
BOOST_TEST(c == 0);
v.clear();
BOOST_TEST(c == 0);
v.resize(5);
BOOST_TEST(c == 0);
v.front() = 1;
}
{
int c = 0;
std::vector<type, boost::noinit_adaptor<creator<type> > > v(&c);
BOOST_TEST(c == 0);
v.push_back(1);
BOOST_TEST(v.front().value() == 1);
v.push_back(2);
BOOST_TEST(c == 0);
v.clear();
BOOST_TEST(c == 0);
v.resize(5);
BOOST_TEST(c == 0);
v.front() = 1;
}
return boost::report_errors(); return boost::report_errors();
} }