Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created September 2, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxpor/62cae843bc2d698bf33d73b91b585814 to your computer and use it in GitHub Desktop.
Save haxpor/62cae843bc2d698bf33d73b91b585814 to your computer and use it in GitHub Desktop.
"std::vector" won't use your class's move unless it's marked noexcept. https://stackoverflow.com/a/10788136/571227
#if __cplusplus >= 201103L
private:
// Constant-time move assignment when source object's memory can be
// moved, either because the source's allocator will move too
// or because the allocators are equal.
void
_M_move_assign(vector&& __x, std::true_type) noexcept
{
vector __tmp(get_allocator());
this->_M_impl._M_swap_data(__tmp._M_impl);
this->_M_impl._M_swap_data(__x._M_impl);
std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
}
// Do move assignment when it might not be possible to move source
// object's memory, resulting in a linear-time operation.
void
_M_move_assign(vector&& __x, std::false_type)
{
if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
_M_move_assign(std::move(__x), std::true_type());
else
{
// The rvalue's allocator cannot be moved and is not equal,
// so we need to individually move each element.
this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
std::__make_move_if_noexcept_iterator(__x.end()));
__x.clear();
}
}
#endif
/**
* @defgroup metaprogramming Metaprogramming
* @ingroup utilities
*
* Template utilities for compile-time introspection and modification,
* including type classification traits, type property inspection traits
* and type transformation traits.
*
* @{
*/
/// integral_constant
template<typename _Tp, _Tp __v>
struct integral_constant
{
static constexpr _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant<_Tp, __v> type;
constexpr operator value_type() const noexcept { return value; }
#if __cplusplus > 201103L
#define __cpp_lib_integral_constant_callable 201304
constexpr value_type operator()() const noexcept { return value; }
#endif
};
template<typename _Tp, _Tp __v>
constexpr _Tp integral_constant<_Tp, __v>::value;
/// The type used as a compile-time boolean with true value.
typedef integral_constant<bool, true> true_type;
/// The type used as a compile-time boolean with false value.
typedef integral_constant<bool, false> false_type;
template<bool __v>
using __bool_constant = integral_constant<bool, __v>;
#if __cplusplus > 201402L
# define __cpp_lib_bool_constant 201505
template<bool __v>
using bool_constant = integral_constant<bool, __v>;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment