Document number: N4151
Date: 2014-08-08
Project: Programming Language C++, Library Evolution Working Group
Reply-to: Agustín Bergé agustinberge@gmail.com
This paper proposes making reference_wrapper<T>
be TriviallyCopyable
.
Trivially copyable types make ideal candidates for the small buffer optimization. So does reference_wrapper<T>
. reference_wrapper<T>
should be trivially copyable.
The simplest way to implement the small buffer optimization is by restricting it to trivially copyable types. Once objects of trivially copyable types are placed into suitable storage, the job is done as the compiler will fill in the details. With this rationale, the following trait can be used to decide when the optimization can be applied:
template <class T, class Buffer>
struct use_small_buffer_optimization
: std::integral_constant<bool,
sizeof(T) <= sizeof(Buffer)
&& alignof(Buffer) % alignof(T) == 0
&& std::is_trivially_copyable<T>::value
&& std::is_copy_constructible<T>::value
> {};
This may or may not include reference_wrapper<T>
, given that whether such template class is trivially copyable is unspecified. Yet reference_wrapper<T>
would be an ideal candidate for the optimization, to the point that it is guaranteed to be used in std::function
. Users may expect reference_wrapper<T>
to be trivially copyable given that it is just a pointer in disguise. Depending on their standard library implementation, it will be trivially copyable, leading to unexpected allocations and possibly exceptions when switching to a different implementation.
The following is a trivial implementation of reference_wrapper
, modulo the call wrapper related functionality:
template <class T>
class reference_wrapper {
public:
// types
typedef T type;
// construct/copy/destroy
reference_wrapper(T& ref) noexcept : _ptr(std::addressof(ref)) {}
reference_wrapper(T&&) = delete;
reference_wrapper(const reference_wrapper&) noexcept = default;
// assignment
reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;
// access
operator T& () const noexcept { return *_ptr; }
T& get() const noexcept { return *_ptr; }
private:
T* _ptr;
};
Particular attention should be given to the fact that the copy constructor and copy assignment operator are not user-provided. They are explicitly defaulted on the first declaration, another alternative is to have them be implicitly declared. Given that reference_wrapper
has no virtual functions and no subobjects of class type, the implicit definitions provided by the implementation will be trivial. With no non-trivial special member functions and a trivial destructor, this implementation of reference_wrapper
is trivially copyable.
The proposed change incurs no breaking changes, given that whether reference_wrapper<T>
is trivially copyable is currently unspecified.
Some ABIs, like the Itanium ABI [ITA], treat types with non-trivial copy constructors differently. They require the caller to allocate space for a temporary copy in which to store the parameter or return value, and pass a pointer to the temporary to the function. This is thus a potential ABI breaking change for those implementations in which reference_wrapper<T>
is not already TriviallyCopyable
. The situation, at the point of this writing, is the following:
libc++ implementation is TriviallyCopyable
.
libstdc++ implementation is not TriviallyCopyable
, but the C++11 ABI is still in flux.
msvc implementation is not TriviallyCopyable
, but does not guarantee ABI compatibility between major releases.
(annecdotical) Boost implementation is TriviallyCopyable
.
Change paragraph 1 of subclause 20.9.3 [refwrap] as shown (relative to [N3936]):
reference_wrapper<T>
is a TriviallyCopyable
, CopyConstructible
and CopyAssignable
wrapper around a reference to an object or function of type T
.[ITA] Itanium C++ ABI http://mentorembedded.github.io/cxx-abi/abi.html
[N3936] ISO/IEC JTC1 SC22 WG21, Programming Languages - C++, working draft, March 2014 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3936.pdf