In C++17 Qt5.15 snippet, I have a QHash of <int, std::unique_ptr>, I can access the values with constFind but the build errors with error C2280: 'std::unique_ptr<MyClass,std::default_delete>::unique_ptr(const std::unique_ptr<MyClass,std::default_delete> &)': attempting to reference a deleted function
I don't want to use shared_ptr just because the QHash is stubborn like this, what would you suggest?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a common issue when using std::unique_ptr as the value type in Qt containers like QHash or QMap. The root cause is that Qt containers, by default, require the value type to be copyable because they may need to copy values internally (e.g., when resizing, rehashing, etc.). However, std::unique_ptr is not copyable (only movable), so you get the error about the deleted copy constructor.
Why does constFind cause this?
constFind itself should not copy the value, but depending on how you use the result, you might be triggering a copy. For example, if you do:
cpp
auto it = myHash.constFind(key);
auto value = *it; // <-- This tries to copy the unique_ptr!
This will try to copy the std::unique_ptr, which is not allowed.
Solutions
1. Use Pointers Directly (Raw or Smart)
If you want to keep using std::unique_ptr, always work with references or pointers to the stored objects, not copies of the unique_ptr itself.
Example:
cpp
QHash<int, std::unique_ptr<MyClass>> myHash;
// Insert
myHash.insert(1, std::make_unique<MyClass>());
// Access
auto it = myHash.constFind(1);
if (it != myHash.constEnd()) {
const MyClass* ptr = it.value().get();
// Use ptr, but do not try to copy the unique_ptr
}
Never do:
cpp
auto value = *it; // BAD: tries to copy unique_ptr
2. Use QHash<int, MyClass*> and Manage Ownership Yourself
If you don't want to use shared_ptr and want to avoid the copy issue, you can store raw pointers and manage their lifetime manually (e.g., delete them in the destructor or when removing from the hash).
Example:
cpp
QHash<int, MyClass*> myHash;
// Insert
myHash.insert(1, new MyClass());
// Access
auto it = myHash.constFind(1);
if (it != myHash.constEnd()) {
MyClass* ptr = it.value();
// Use ptr
}
// Cleanup (e.g., in destructor)
qDeleteAll(myHash);
myHash.clear();
3. Use QHash<int, std::shared_ptr<MyClass>>
If you want automatic memory management and copyability, std::shared_ptr is the easiest solution, but you said you want to avoid it.
4. Use QHash<int, QScopedPointer<MyClass>> (Qt's unique pointer)
QScopedPointer is also non-copyable, so the same rules as std::unique_ptr apply.
5. Use QHash<int, std::unique_ptr<MyClass>> with Move Semantics
If you need to move values in and out, use std::move explicitly, but never try to copy.
Summary
Do not try to copy the value from the hash. Only use references or pointers to the object.
If you need copyable semantics, use shared_ptr or raw pointers.
If you want unique ownership, stick with unique_ptr and be careful with how you access and use the values.
Example: Correct Usage
cpp
QHash<int, std::unique_ptr<MyClass>> myHash;
myHash.insert(1, std::make_unique<MyClass>());
auto it = myHash.constFind(1);
if (it != myHash.constEnd()) {
// Use the pointer, do not copy the unique_ptr
const MyClass* obj = it.value().get();
obj->doSomething();
}
If you show your access code, I can help you refactor it to avoid the copy!
User
well, the issue doesn't arise when rerturning the value (which is a reference) but here:
code
template <typename ...Args>
iterator emplace(Key &&key, Args &&... args)
{
if (isDetached()) {
if (d->shouldGrow()) // Construct the value now so that no dangling references are used
return emplace_helper(std::move(key), T(std::forward<Args>(args)...));
return emplace_helper(std::move(key), std::forward<Args>(args)...);
}
// else: we must detach
const auto copy = *this; // keep 'args' alive across the detach/growth
detach();
return emplace_helper(std::move(key), std::forward<Args>(args)...);
}
Assistant
Great, you’ve found the real culprit:
cpp
const auto copy = *this; // keep 'args' alive across the detach/growth
This line is copying the entire QHash, which means it tries to copy all the std::unique_ptr<MyClass> values inside. Since std::unique_ptr is not copyable, this fails.
This is internal Qt code (from QHash or QMap), and it means that Qt containers are not compatible with non-copyable value types like std::unique_ptr (or QScopedPointer, etc.), because they sometimes need to copy the container (for COW, detaching, etc.).
What are your options?
1. Don’t use std::unique_ptr as the value type in QHash
This is the most robust solution. Use a pointer type that is copyable, or use raw pointers and manage the memory yourself.
Example:
cpp
QHash<int, MyClass*> myHash;
myHash.insert(1, new MyClass);
// ... remember to delete them later!
If you really want to use unique_ptr-like semantics, you can wrap it in a custom class that is copyable (by cloning the object), but this is rarely worth the trouble.
3. Use std::map or std::unordered_map instead
The STL containers do not use copy-on-write and do not copy the container internally, so you can use std::unique_ptr as the value type.
If you need Qt’s signals/slots, use QSharedPointer or raw pointers.
If you want unique ownership, use std::unordered_map or std::map with std::unique_ptr.
TL;DR
You cannot use std::unique_ptr as the value type in QHash or QMap because Qt containers require copyable value types. Use STL containers or a copyable pointer type instead.
If you want a code migration example, let me know your use case and I can help you refactor!