C++: error: use of deleted function 'X::X(const X&)' — copying a non-copyable type
The code needs a function (usually a copy constructor or assignment) that was deleted explicitly or implicitly, as for std::unique_ptr, std::mutex or classes holding them.
use of deleted functionis implicitly deleted because the default definition would be ill-formedcall to implicitly-deleted copy constructorattempting to reference a deleted functionuse of deleted function 'std::unique_ptr Meaning
A type becomes non-copyable when a member is non-copyable, when it has a user-declared move operation, or when = delete is written. Passing it by value, returning a local from a lambda, or storing it in a container that copies triggers the error. Move it, pass by reference or pointer, or make the type copyable.
Common causes
- Copying a
std::unique_ptr,std::mutex,std::threadorstd::ifstream - Class with a non-copyable member is copied implicitly
- Constructor or assignment explicitly
= delete - Passing the object by value to a function or lambda capture by copy
⚡ Quick fix
- Pass by reference (
const T&) or by pointer - Use
std::move(obj)to transfer ownership - Capture with
[&]or[p = std::move(p)]in lambdas
Detailed fix by platform
C++
- Replace copies by moves:
consume(std::move(ptr)); - Change
void f(Widget w)tovoid f(const Widget& w) - If copying is really wanted, use
std::shared_ptror add a copy constructor
Code examples
unique_ptr cannot be copied
#include <memory>
void take(std::unique_ptr<int> p);
int main() {
auto a = std::make_unique<int>(42);
// take(a); // error: use of deleted function 'unique_ptr(const unique_ptr&)'
take(std::move(a)); // OK: ownership moves, 'a' is now empty
}How to diagnose
- Type — Which type's copy is deleted?
- Call site — Where is a copy made implicitly?
- Intent — Should it be moved, shared or referenced?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
- discards qualifiers C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive]
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026