C++

Fix C++ "Use of Deleted Function" — Copying a unique_ptr Holder, Deleted Copies, and Deleted Overloads

The compiler deletes a function when the program asks for something the type cannot do — copy a unique owner, for example — or when the code says so with = delete. Either way the build stops at the use, and the error names the function it refused. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.

Written November 20, 2026 · Tested with GCC 13.3.0 · 13 min read

By the end of this guide
  • The first error: a class that holds a uniqueptr
  • The fix that moves: std::move
  • When the original has to stay alive: a clone
  • An explicitly deleted copy

Written November 20, 2026

Tested with GCC 13.3.0

What this guide is

use of deleted function is the build saying a copy, a call, or an overload landed on a function the program must not use. A function becomes deleted in two ways. The code can delete it on purpose, with = delete after the declaration. Or the compiler can delete it on its own: a copy constructor is implicitly deleted when a member of the class cannot be copied, and the usual member that cannot be copied is a std::unique_ptr, which owns its object alone and has no usable copy constructor of its own. Any use of a deleted function is ill-formed, so the program does not compile — it is not a warning, and there is no flag that turns it into one.

The examples here use C++17 and GCC 13.3.0. Each program is a separate file in one directory, compiled with g++ -std=c++17 and the file name. The diagnostics below are shown as GCC prints them under a UTF-8 locale, which is the default on most systems; under a C or POSIX locale the same messages appear with straight quotes instead of curly ones. A failing compile prints its diagnostic to standard error and exits with status 1; a passing program is run, and its output is shown exactly as printed. Each section's failing program isolates one shape, so the diagnostic in that section belongs to that shape and nothing else.

The first error: a class that holds a unique_ptr

The most common shape of this error has no = delete anywhere in the program. Holder holds a std::unique_ptr<std::string>, declares no copy constructor, and the program copies a Holder:

C++
#include <memory>
#include <string>
#include <utility>

struct Holder {
    std::unique_ptr<std::string> text;
    explicit Holder(std::string value)
        : text(std::make_unique<std::string>(std::move(value))) {}
};

int main() {
    Holder first("hello");
    Holder second = first;
    return 0;
}
Terminal
$ g++ -std=c++17 holder.cpp -o holder
holder.cpp: In function ‘int main()’:
holder.cpp:13:21: error: use of deleted function ‘Holder::Holder(const Holder&)’
   13 |     Holder second = first;
      |                     ^~~~~
holder.cpp:5:8: note: ‘Holder::Holder(const Holder&)’ is implicitly deleted because the default definition would be ill-formed:
    5 | struct Holder {
      |        ^~~~~~
holder.cpp:5:8: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::__cxx11::basic_string<char>; _Dp = std::default_delete<std::__cxx11::basic_string<char> >]’
In file included from /usr/include/c++/13/memory:78,
                 from holder.cpp:1:
/usr/include/c++/13/bits/unique_ptr.h:522:7: note: declared here
  522 |       unique_ptr(const unique_ptr&) = delete;
      |       ^~~~~~~~~~

The error names Holder's copy constructor first, at line 13, the line that copies first into second. The note under it says that copy constructor is implicitly deleted, and the second error, attached to the same line 5, names the cause: the copy constructor of std::unique_ptr<std::string>, deleted at line 522 of the header. The chain is the whole diagnosis. Holder declares no copy constructor, so the compiler declares one for it; that declared copy constructor would have to copy each member, and the unique_ptr member cannot be copied, so the declared copy constructor is defined as deleted. Copy-initialization from an lvalue needs the copy constructor — Holder second = first; asks for it — and the deleted function is what the compiler finds.

The fix depends on what the program needs the copy for. If the original does not have to stay usable, move it. If it does, build a second object that owns its own copy of the data. Both fixes are below, starting with the move.

The fix that moves: std::move

std::move turns the named object into an rvalue, so overload resolution selects the move constructor instead of the copy constructor. Holder has an implicit move constructor, because it declares no copy constructor, no copy assignment operator, no move assignment operator, and no destructor. The move constructor moves the unique_ptr member, which transfers ownership of the string:

C++
#include <iostream>
#include <memory>
#include <string>
#include <utility>

struct Holder {
    std::unique_ptr<std::string> text;
    explicit Holder(std::string value)
        : text(std::make_unique<std::string>(std::move(value))) {}
};

int main() {
    Holder first("hello");
    Holder second = std::move(first);
    std::cout << *second.text << '\n';
    return 0;
}
Terminal
$ g++ -std=c++17 holder_move.cpp -o holder_move
$ ./holder_move
hello

After the move, second owns the string and prints it. first is left empty: a moved-from unique_ptr owns nothing, so nothing in the rest of the program may read through first.text after this line. The move is the right fix only when the original is finished with — when the program moves a value into a container, returns it, or hands it to a function that takes ownership. When the original still has work to do, the move trades one error for a use of an empty pointer, and the clone below is the fix instead.

When the original has to stay alive: a clone

When both objects have to stay usable, the program needs a second object that owns its own string, not a second handle on the same one. A clone member builds that object from the pointed-to string:

C++
#include <iostream>
#include <memory>
#include <string>
#include <utility>

struct Document {
    std::unique_ptr<std::string> title;
    explicit Document(std::string value)
        : title(std::make_unique<std::string>(std::move(value))) {}
    Document clone() const {
        return Document(*title);
    }
};

int main() {
    Document first("draft");
    Document second = first.clone();
    std::cout << *second.title << '\n';
    return 0;
}
Terminal
$ g++ -std=c++17 document_clone.cpp -o document_clone
$ ./document_clone
draft

clone is a const member, so calling it copies nothing about the Document itself. It reads the string through the pointer and constructs a fresh Document from that string, and the fresh object owns its own unique_ptr to its own copy of the text. The two objects are independent after that: changing one string leaves the other alone. This is the fix to reach for when the copy in the failing program was a real requirement — the program genuinely needs two copies of the data — rather than an accident of passing a value by copy. When the copy was an accident, the move above, or the reference in the next section, is the smaller change.

An explicitly deleted copy

The second shape deletes the copy on purpose. Token deletes its copy constructor and its copy assignment operator on their first declarations, and a function takes a Token by value:

C++
#include <iostream>

struct Token {
    Token() = default;
    Token(const Token&) = delete;
    Token& operator=(const Token&) = delete;
};

void use(Token token) {
    (void)token;
}

int main() {
    Token token;
    use(token);
    std::cout << "done\n";
    return 0;
}
Terminal
$ g++ -std=c++17 token.cpp -o token
token.cpp: In function ‘int main()’:
token.cpp:15:8: error: use of deleted function ‘Token::Token(const Token&)’
   15 |     use(token);
      |     ~~~^~~~~~~
token.cpp:5:5: note: declared here
    5 |     Token(const Token&) = delete;
      |     ^~~~~
token.cpp:9:16: note:   initializing argument 1 of ‘void use(Token)’
    9 | void use(Token token) {
      |          ~~~~~~^~~~~

The diagnostic is shorter this time, because the deletion is explicit and there is no chain to unwind. The error names Token's copy constructor at line 15, the call to use. One note points at the = delete declaration on line 5; a second note points at the parameter of use on line 9, where the copy would be initialized. The call passes token by value, and passing by value copies the argument into the parameter, so the call needs the copy constructor that the class deleted.

A deleted definition must be the first declaration of the function — a function declared earlier in the translation unit cannot be redeclared as deleted afterward. Token deletes on the first declaration, so the deletion itself is well formed; the error is the use, not the declaration.

The fix that takes a reference

When the function only needs to look at the token, not own a copy of it, the fix is to take the parameter by reference instead of by value:

C++
#include <iostream>

struct Token {
    Token() = default;
    Token(const Token&) = delete;
    Token& operator=(const Token&) = delete;
};

void use(const Token& token) {
    (void)token;
}

int main() {
    Token token;
    use(token);
    std::cout << "done\n";
    return 0;
}
Terminal
$ g++ -std=c++17 token_ref.cpp -o token_ref
$ ./token_ref
done

No copy happens anywhere in this program. The parameter binds directly to the token in main, the function reads it through the reference, and the deleted copy constructor is never selected. The class is unchanged: a Token still cannot be copied anywhere else in the program, and any other code that tries to copy one gets the same error this section started with. That is the point of deleting the copy — the type still cannot be copied, and the call sites that do not need a copy stop asking for one. If a call site genuinely needs its own Token, the reference is the wrong fix, and the type needs a clone like the one in the Document section, or a move constructor that transfers whatever the token owns.

A move-only argument passed by value

The third shape needs no class of its own. The argument itself is move-only: take takes a std::unique_ptr<int> by value, and main passes a named unique_ptr to it:

C++
#include <iostream>
#include <memory>

void take(std::unique_ptr<int> ptr) {
    std::cout << *ptr << '\n';
}

int main() {
    auto value = std::make_unique<int>(42);
    take(value);
    return 0;
}
Terminal
$ g++ -std=c++17 owner.cpp -o owner
owner.cpp: In function ‘int main()’:
owner.cpp:10:9: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’
   10 |     take(value);
      |     ~~~~^~~~~~~
In file included from /usr/include/c++/13/memory:78,
                 from owner.cpp:2:
/usr/include/c++/13/bits/unique_ptr.h:522:7: note: declared here
  522 |       unique_ptr(const unique_ptr&) = delete;
      |       ^~~~~~~~~~
owner.cpp:4:32: note:   initializing argument 1 of ‘void take(std::unique_ptr<int>)’
    4 | void take(std::unique_ptr<int> ptr) {
      |           ~~~~~~~~~~~~~~~~~~~~~^~~

The diagnostic names the deleted function directly — the copy constructor of std::unique_ptr<int> — because the deleted function belongs to unique_ptr itself, not to a class in this file. The note points at the = delete in the header, and a second note points at the parameter of take on line 4, where the copy would be initialized. A named variable is an lvalue, and passing an lvalue by value copies it, so the call asks for the copy constructor that unique_ptr deletes. This is the same rule as the Holder section, with the member removed: the error is about the argument, not about a class holding it.

The fix that moves the argument

Move the argument at the call site, and ownership passes into the parameter:

C++
#include <iostream>
#include <memory>
#include <utility>

void take(std::unique_ptr<int> ptr) {
    std::cout << *ptr << '\n';
}

int main() {
    auto value = std::make_unique<int>(42);
    take(std::move(value));
    return 0;
}
Terminal
$ g++ -std=c++17 owner_move.cpp -o owner_move
$ ./owner_move
42

std::move(value) turns the named pointer into an rvalue, so the move constructor is selected and ownership of the integer transfers into take's parameter. The function prints the value it now owns, and value in main is empty after the call. A function that takes a unique_ptr by value takes ownership — that is the shape this parameter declares, and the call site has to match it by moving. If the function should not take ownership, the parameter should be a reference or a raw pointer to the managed object instead, and the call site passes the pointer without moving: take(value.get()) for a function taking an int*, or take(*value) for a function taking a const int&. Which of those is right depends on what the function does with the object; the move is right when the function is meant to own it.

Overload resolution picks the deleted overload

The fourth shape has a deleted function that nothing in the program calls by name. Two overloads of take exist, one for int and one for double, and the double overload is deleted:

C++
#include <iostream>

void take(int value) {
    std::cout << value << '\n';
}

void take(double value) = delete;

int main() {
    double score = 9.5;
    take(score);
    return 0;
}
Terminal
$ g++ -std=c++17 score.cpp -o score
score.cpp: In function ‘int main()’:
score.cpp:11:9: error: use of deleted function ‘void take(double)’
   11 |     take(score);
      |     ~~~~^~~~~~~
score.cpp:7:6: note: declared here
    7 | void take(double value) = delete;
      |      ^~~~

The call passes a double, and the double overload is an exact match, so overload resolution selects it — even though the int overload exists and would have worked after a conversion. The rule is that overload resolution runs first, and the program is ill-formed only if the deleted function was selected; here it was. The error names void take(double) at line 11, the call, and the note points at the = delete declaration on line 7. Nothing is wrong with the int overload, and nothing is wrong with the argument in isolation. The call and the overload set together select a deleted function, and that selection is the error.

This shape usually appears as a guard: a double overload deleted on purpose to stop a floating-point value from narrowing silently into an int parameter. The guard works — the build stops. The fix depends on whether the call is a mistake or the guard is.

The fix that removes the deleted overload

If the call is legitimate and double values should be accepted, remove the deleted overload. With it gone, the call converts 9.5 to 9 and selects the int overload:

C++
#include <iostream>

void take(int value) {
    std::cout << value << '\n';
}

int main() {
    double score = 9.5;
    take(score);
    return 0;
}
Terminal
$ g++ -std=c++17 score_int.cpp -o score_int
$ ./score_int
9

Removing the overload also removes the guard: every other double passed to take now narrows silently instead of failing the build. If the guard should stay for every other call, keep the overload and convert the argument explicitly at the one call site that means it:

C++
#include <iostream>

void take(int value) {
    std::cout << value << '\n';
}

void take(double value) = delete;

int main() {
    double score = 9.5;
    take(static_cast<int>(score));
    return 0;
}
Terminal
$ g++ -std=c++17 score_cast.cpp -o score_cast
$ ./score_cast
9

The explicit conversion says the narrowing is intended, at this call and nowhere else, and the deleted overload keeps catching accidental double arguments everywhere else. Choose between the two by deciding which side is wrong: if the call is right and the guard is in the way, remove the overload; if the guard is right and this call is the exception, convert at the call site.

The exact rule: where a deleted function comes from

A deleted function is a function the program must not use. It can be deleted explicitly, with = delete on its first declaration, or implicitly, when the compiler declares a special member function that the class cannot support: the copy constructor is defined as deleted when a member or base cannot be copied, and the move constructor is defined as deleted when a member or base cannot be moved. std::unique_ptr is a member that commonly triggers the implicit case, because it satisfies the move requirements and neither of the copy requirements — it can be moved, never copied.

The implicit move constructor exists only when the class declares none of the operations that would suppress it: no user-declared copy constructor, copy assignment operator, move assignment operator, or destructor. Holder and Document declare none of those, so both get an implicit move constructor, and that is why std::move fixes their errors. A class that declares a destructor or a copy operation does not get one, and moving such a class falls back to its copy constructor — which, for a class holding a unique_ptr, is deleted. In that case the fix is to declare the move constructor explicitly, with = default if the member-wise move is the right one.

For overloads, resolution runs first and only the selected function matters. A deleted overload that no call selects is harmless, and a live overload that no call selects does not save a call that selects the deleted one. That is why the score program fails while a program that only ever passes int values to the same two overloads compiles: the deleted overload is selected by one call and ignored by the other.

The fixes, ranked

  • First, read the function name in the error. Holder::Holder means the copy belongs to that class; std::unique_ptr<...>::unique_ptr means the copy belongs to the pointer itself; void take(double) means an overload was selected.
  • Second, find where the copy is requested: an initialization like Holder second = first;, a by-value parameter, or a return of a named object. The line the error points at is the request, not the deletion.
  • Third, if the copied thing is a member that cannot copy, move it when the original is finished with, or clone it when the original has to stay alive.
  • Fourth, if a by-value parameter takes a move-only type, move the argument when the function should own it, or change the parameter to a reference when it should only look at it.
  • Fifth, if a deleted overload was selected, decide which side is wrong: remove the overload when the call is legitimate, or convert the argument explicitly at the call site when the guard should stay.

Which fix for which cause

Cause Message Fix
Class holding a unique_ptr, copied use of deleted function 'Holder::Holder(...)' Move the original, or add a clone
Copy constructor deleted with = delete, passed by value use of deleted function 'Token::Token(...)' Take the parameter by const Token&
Move-only argument passed by value use of deleted function 'unique_ptr(...)' Move the argument at the call site
Deleted overload is the best match use of deleted function 'void take(double)' Remove the overload, or convert at the call site

Checklist

  • Read the function name in the error first: it says whether the deleted copy belongs to your class, to a unique_ptr, or to a selected overload.
  • Read the line the error points at: that line requests the copy or makes the call, and it is the line to change.
  • Check whether the original has to stay usable before moving it: a moved-from unique_ptr is empty, and reading through it after the move is a use of an empty pointer.
  • Pass a move-only type by value only when the function should take ownership; otherwise take a reference, or pass the raw pointer or the pointed-to object.
  • Decide, for a deleted overload, whether the call or the guard is wrong: remove the overload when the call is legitimate, convert explicitly at the call site when the guard should stay.
  • Remember that declaring a destructor or a copy operation suppresses the implicit move constructor: a class holding a unique_ptr that declares either needs its move constructor declared too.

Sources

  • Function declaration — for deleted functions (any use of a deleted function is ill-formed, including implicit calls and uses in expressions that are not potentially evaluated), for overload resolution taking place first and the program being ill-formed only if the deleted function was selected, and for a deleted definition having to be the first declaration in a translation unit.
  • Copy constructors — for the implicitly-declared copy constructor, for it being defined as deleted when a subobject's copy constructor does not result in a usable candidate, and for the copy constructor being called on initialization from another object of the same type, on argument passing, and on return.
  • Move constructors — for the move constructor being called on initialization from an rvalue, including std::move(b), for the implicitly-declared move constructor existing only when there is no user-declared copy constructor, copy assignment operator, move assignment operator, or destructor, and for move constructors transferring resources and leaving the argument in a valid but indeterminate state.
  • std::unique_ptr — for unique_ptr satisfying the MoveConstructible and MoveAssignable requirements but neither CopyConstructible nor CopyAssignable, and for passing ownership of uniquely-owned objects into and out of functions.

Keep Exploring