use of deleted function C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see 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
Seen on: C/C++ GCC Clang

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::thread or std::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

  1. Pass by reference (const T&) or by pointer
  2. Use std::move(obj) to transfer ownership
  3. Capture with [&] or [p = std::move(p)] in lambdas

Detailed fix by platform

C++

  1. Replace copies by moves: consume(std::move(ptr));
  2. Change void f(Widget w) to void f(const Widget& w)
  3. If copying is really wanted, use std::shared_ptr or add a copy constructor

Code examples

unique_ptr cannot be copied

cpp
#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

  1. Type — Which type's copy is deleted?
  2. Call site — Where is a copy made implicitly?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…discards qualifiers — C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive] (C/C++)discards qualifiersdouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…incomplete type — C++: error: invalid use of incomplete type 'class X' — forward declaration only (C/C++)incomplete typeis private within this context — C++: error: 'int X::secret' is private within this context (C/C++)is private within thi…no matching function for call to — C++: error: no matching function for call to 'f(int, const char*)' (C/C++)no matching function …use of de…

Explore the map of this error Full error map →

🧠 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.