mirror of
https://github.com/boostorg/core.git
synced 2025-07-30 21:07:22 +02:00
Add noinit_adapt() free function utility
This commit is contained in:
@ -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]
|
||||||
|
@ -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
|
||||||
|
@ -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_;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
inline bool
|
||||||
|
operator==(const type& lhs, const type& rhs)
|
||||||
{
|
{
|
||||||
|
return lhs.value() == rhs.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class A>
|
||||||
|
void test(const A& allocator)
|
||||||
{
|
{
|
||||||
int c = 0;
|
std::vector<int, A> v(allocator);
|
||||||
std::vector<int, boost::noinit_adaptor<creator<int> > > v(&c);
|
|
||||||
BOOST_TEST(c == 0);
|
|
||||||
v.push_back(1);
|
v.push_back(1);
|
||||||
BOOST_TEST(v.front() == 1);
|
BOOST_TEST(v.front() == 1);
|
||||||
v.push_back(2);
|
|
||||||
BOOST_TEST(c == 0);
|
|
||||||
v.clear();
|
v.clear();
|
||||||
BOOST_TEST(c == 0);
|
|
||||||
v.resize(5);
|
v.resize(5);
|
||||||
BOOST_TEST(c == 0);
|
|
||||||
v.front() = 1;
|
v.front() = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
{
|
{
|
||||||
int c = 0;
|
test(boost::noinit_adaptor<creator<int> >(1));
|
||||||
std::vector<type, boost::noinit_adaptor<creator<type> > > v(&c);
|
test(boost::noinit_adaptor<creator<type> >(2));
|
||||||
BOOST_TEST(c == 0);
|
test(boost::noinit_adapt(creator<int>(3)));
|
||||||
v.push_back(1);
|
test(boost::noinit_adapt(creator<type>(4)));
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user