Since C++17, how can a C++ make_report() factory return a Report that holds a std::mutex, and when does it still fail to compile?
answer
- std::mutex is neither copyable nor movable
- return a prvalue
- a named local needs a constructor
- where the object can go afterwards
basics
~20 sReturn a prvalue: return Report{n}; initializes the caller's object directly, so C++17 needs no copy or move constructor. Returning a named local still fails, because NRVO is optional and a usable copy or move constructor must exist.
solid answer
~40 s`std::mutex` deletes its copy constructor, which also leaves it without a move constructor, so a `Report` with a `std::mutex` member is neither copyable nor movable. Before C++17, `return Report{42};` failed to compile anyway because the language still required an accessible copy or move constructor for the elided temporary. Since C++17 a prvalue initializes the result object directly, so `Report make_report(int n) { return Report{n}; }` and `auto r = make_report(42);` compile and construct exactly one object. It still fails when the function builds a named local and returns it, because NRVO is optional and the copy-initialization must be valid; Clang reports `call to implicitly-deleted copy constructor of 'Report'`. Afterwards the object cannot be moved either: `std::vector<Report>::emplace_back` needs move-insertable elements, while `std::deque::emplace_back` and `std::optional::emplace` construct in place and work.
code
cpp · 16 lines#include <atomic>
#include <cstdio>
#include <mutex>
struct Report {
std::mutex lock; // std::mutex is neither copyable nor movable
std::atomic<int> rows{0};
explicit Report(int n) : rows(n) {}
};
Report make_report(int n) { return Report{n}; } // OK since C++17: prvalue, no move needed
int main() {
Report r = make_report(42); // r is initialized directly by the return statement
std::printf("rows=%d\n", r.rows.load());
}go deeper
Know that since C++17 a function can return Report{...} even when Report holds a std::mutex, because nothing is copied or moved.
Explain why returning a named local of the same type still fails, and why std::vector cannot hold the result while std::deque can.
Design factories for pinned types: compute inputs, return a prvalue, and wrap in std::unique_ptr when the object must change owners later.
Decide when a type should be deliberately non-movable, and what that costs callers in container choice and ownership models.
## Why `Report` cannot be moved `std::mutex` declares `mutex(const mutex&) = delete;` and `operator=(const mutex&) = delete;`. A user-declared copy constructor, even a deleted one, means no move constructor is declared, so `std::mutex` is **neither copyable nor movable**. A class with a `std::mutex` member inherits that: its implicit copy constructor is defined as deleted, and it has no usable move constructor. (`std::atomic<int>` behaves the same way.) ## What C++17 made possible Before C++17, `return Report{42};` conceptually created a temporary and copy-initialized the result from it. The copy was usually elided, but the program still had to be valid as if it were not, so Clang in `-std=c++14` mode reports `call to implicitly-deleted copy constructor of 'Report'` on that line. C++17 defines a returned prvalue of the same type as **directly initializing** the result object. There is no temporary, no copy and no move to check. So: - `Report make_report(int n) { return Report{n}; }` compiles; - `Report r = make_report(42);` and `auto r = make_report(42);` construct exactly one `Report`, in `r`; - passing `make_report(7)` to a function that takes `Report` by value initializes the parameter directly too; - `return big ? Report{1000} : Report{10};` also works, because a conditional between two prvalues of the same type is itself a prvalue. ## When it still fails 1. **A named local.** `Report r; r.rows = n; return r;` asks for NRVO, which is optional, so the copy-initialization must be valid. With both constructors unusable, Clang reports the implicitly-deleted copy constructor and adds a note that `field 'lock' has a deleted copy constructor`. 2. **Assigning later.** `Report r = make_report(1); r = make_report(2);` needs an assignment operator, which `std::mutex` also deletes. 3. **Moving it into a `std::vector`.** `std::vector<Report>::emplace_back` requires the element to be *Cpp17MoveInsertable* as well as emplace-constructible, because growth relocates elements. libc++ rejects it with a `static_assert` naming `Cpp17MoveInsertable`. ## Where such an object can live | Destination | Works? | Why | |---|---|---| | a local initialized from `make_report()` | yes | guaranteed elision | | a by-value parameter initialized from `make_report()` | yes | the prvalue initializes the parameter | | `std::deque<Report>::emplace_back(5)` | yes | only emplace-constructible is required, and elements never relocate | | `std::optional<Report>::emplace(7)` | yes | constructs in place inside the optional | | `std::vector<Report>::emplace_back(5)` | no | growth needs move-insertable elements | | `std::unique_ptr<Report>` from `std::make_unique<Report>(5)` | yes | the object stays put; only the pointer moves | ## What the caller can do with the result Because the object can never move, everything that would relocate it must be avoided by the caller as well: - bind it once, at its final home: `const auto& r = make_report(1);` works too, because the prvalue is materialized into a temporary whose lifetime the reference extends; - return it again from another function only as the same prvalue, `return make_report(n);`, which C++17 also elides; - share it by pointer or reference, never by value, once it exists; - hold several of them in a `std::deque`, a `std::list` or an array of `std::optional<Report>`, all of which construct in place. A test that tries `static_assert(!std::is_move_constructible_v<Report>);` documents the property and stops someone from "fixing" it by adding a move constructor that cannot correctly move a mutex. ## Other non-movable types you will meet The same reasoning applies to every type that deletes both copy and move: - `std::mutex`, `std::recursive_mutex`, `std::shared_mutex` and `std::condition_variable`; - `std::atomic<T>` and `std::atomic_flag`; - user types that `= delete` their copy constructor to pin an address, for example an object registered with a callback system by pointer. For all of them, C++17 lets a factory return a prvalue, and every other route (named local, push into a `std::vector`, assignment) still needs an operation the type does not have. ## Writing the factory so it stays valid - Keep the last statement a prvalue: compute the inputs first, then `return Report{rows, title};`. - If construction needs several steps, give `Report` a constructor that takes the computed parts, rather than mutating a named local. - If the object must later move between owners, wrap it: return `std::unique_ptr<Report>` or put the mutex behind a pointer, and accept the extra allocation. - Document that the type is pinned in memory, since that is exactly what a non-movable member such as a mutex demands.
- Can a C++17 factory build a non-movable object step by step and still return it?Not as a named local that it mutates and then returns, because that relies on optional NRVO and needs a usable copy or move constructor. Move the steps into computing constructor arguments, or into a constructor or an aggregate initializer, and end with `return Report{...};`. Proposals for guaranteed NRVO exist, but no published standard has adopted one.
- Why does std::deque accept emplace_back of a non-movable type when std::vector does not?A `deque` never relocates existing elements when it grows; it adds new blocks. The standard therefore requires only that the element be emplace-constructible. A `vector` must move its elements into new storage on reallocation, so its `emplace_back` additionally requires the element to be move-insertable, which a type holding a `std::mutex` is not.
- A type holding a std::mutex can never be returned from a function by value.
- C++17 guarantees elision for a named local, so returning it needs no move constructor.
- Guaranteed elision means the object can later be pushed into a std::vector.
- std::mutex is movable, so a Report holding one only needs a move constructor.
- return Report{42}; worked in C++14 for non-movable types because compilers elided the copy.
Assumes C++17 or later: guaranteed elision of a returned prvalue arrived in C++17; under C++14 the same factory fails to compile because the copy or move constructor must be accessible.