mirror of
https://github.com/boostorg/core.git
synced 2025-05-09 23:03:54 +00:00
Simplify exchange implementation for pre-C++11
This commit is contained in:
parent
75ae238d0c
commit
19ec659a91
@ -15,18 +15,10 @@ Distributed under the Boost Software License, Version 1.0.
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
|
||||
template<class T>
|
||||
struct exchange_type {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
} /* detail */
|
||||
|
||||
#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
template<class T>
|
||||
inline T exchange(T& t, const typename detail::exchange_type<T>::type& u)
|
||||
template<class T, class U>
|
||||
inline T exchange(T& t, const U& u)
|
||||
{
|
||||
T v = t;
|
||||
t = u;
|
||||
@ -34,19 +26,11 @@ inline T exchange(T& t, const typename detail::exchange_type<T>::type& u)
|
||||
}
|
||||
#else
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
|
||||
template<class T>
|
||||
inline T exchange(T& t, const typename detail::exchange_type<T>::type& u)
|
||||
template<class T, class U>
|
||||
inline T exchange(T& t, U&& u)
|
||||
{
|
||||
T v = std::move(t);
|
||||
t = u;
|
||||
return v;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T exchange(T& t, typename detail::exchange_type<T>::type&& u)
|
||||
{
|
||||
T v = std::move(t);
|
||||
t = std::move(u);
|
||||
t = std::forward<U>(u);
|
||||
return v;
|
||||
}
|
||||
#else
|
||||
|
@ -59,10 +59,37 @@ void test2()
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
class C3 {
|
||||
public:
|
||||
explicit C3(int i)
|
||||
: i_(i) { }
|
||||
C3(C3&& c)
|
||||
: i_(c.i_) { }
|
||||
C3& operator=(C1&& c) {
|
||||
i_ = c.i();
|
||||
return *this;
|
||||
}
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
C3(const C3&);
|
||||
C3& operator=(const C3&);
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test3()
|
||||
{
|
||||
C3 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
return boost::report_errors();
|
||||
}
|
||||
#else
|
||||
|
@ -56,10 +56,36 @@ void test3()
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
class C3 {
|
||||
public:
|
||||
explicit C3(int i)
|
||||
: i_(i) { }
|
||||
C3(const C3& c)
|
||||
: i_(c.i_) { }
|
||||
C3& operator=(const C1& c) {
|
||||
i_ = c.i();
|
||||
return *this;
|
||||
}
|
||||
int i() const {
|
||||
return i_;
|
||||
}
|
||||
private:
|
||||
C3& operator=(const C3&);
|
||||
int i_;
|
||||
};
|
||||
|
||||
void test4()
|
||||
{
|
||||
C3 x(1);
|
||||
BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
|
||||
BOOST_TEST(x.i() == 2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
test4();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user