Document number: N4277
Project: Programming Language C++
Reply-to: Agustín Bergé email@example.com
TriviallyCopyable reference_wrapper (Revision 1)
Changes since N4151:
- LWG pointed out that
TriviallyCopyableis not a thing, and suggested new wording.
This paper proposes making
Trivially copyable types make ideal candidates for the small buffer optimization. So does
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:
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:
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.
4. Impact on the Standard
The proposed change incurs no breaking changes, given that whether
reference_wrapper<T> is trivially copyable is currently unspecified.
5. Impact on ABI Stability
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
libstdc++ implementation is
msvc implementation is not
TriviallyCopyable, but does not guarantee ABI compatibility between major releases.
(annecdotical) Boost implementation is
6. Proposed Wording
Add the following as a new paragraph after 20.9.3 [refwrap] paragraph 1 (relative to [N4140]):
- [ITA] Itanium C++ ABI http://mentorembedded.github.io/cxx-abi/abi.html
- [N4140] ISO/IEC JTC1 SC22 WG21, Programming Languages - C++, working draft, October 2014 http://www.open-std.org/jtc1/sc22/wg21/prot/14882fdis/n4140.pdf