gpt4 book ai didi

c++ - 将静态函数删除器与 std::unique_ptr 一起使用时出错

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:12 25 4
gpt4 key购买 nike

我有以下无法编译的代码片段......看起来应该是这样,但它现在正在逃避我。非常感谢任何帮助/建议!

#include <atomic>
#include <memory>

struct MyClass {

static void free_lock(std::atomic<int>** lck) { (*lck)->store(0); }

typedef std::unique_ptr<std::atomic<int>*, decltype(&MyClass::free_lock)> lock_scope;

static lock_scope get_lock() {
static std::atomic<int> lck(0);
int ref = 0;
return lock_scope(&lck, &MyClass::free_lock);
}

};

Clang 3.2报如下错误信息

Compilation finished with errors:source.cpp:13:23: error: no matching constructor for initialization of 'lock_scope' (aka 'unique_ptr *, decltype(&MyClass::free_lock)>')        return lock_scope(&lck, &MyClass::free_lock);                         ^          ~~~~~~~~~~~~~~~~~~~~~~~~~/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:130:7: note: candidate constructor not viable: no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') for 1st argument      unique_ptr(pointer __p,      ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:125:7: note: candidate constructor not viable: no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') for 1st argument      unique_ptr(pointer __p,      ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:155:2: note: candidate constructor template not viable: requires single argument '__u', but 2 arguments were provided        unique_ptr(unique_ptr&& __u) noexcept        ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:164:2: note: candidate constructor template not viable: requires single argument '__u', but 2 arguments were provided        unique_ptr(auto_ptr&& __u) noexcept        ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:114:17: note: candidate constructor not viable: requires 0 arguments, but 2 were provided      constexpr unique_ptr() noexcept                ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:120:7: note: candidate constructor not viable: requires single argument '__p', but 2 arguments were provided      unique_ptr(pointer __p) noexcept      ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:136:17: note: candidate constructor not viable: requires 1 argument, but 2 were provided      constexpr unique_ptr(nullptr_t) noexcept                ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:142:7: note: candidate constructor not viable: requires single argument '__u', but 2 arguments were provided      unique_ptr(unique_ptr&& __u) noexcept      ^/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/unique_ptr.h:262:7: note: candidate constructor not viable: requires 1 argument, but 2 were provided      unique_ptr(const unique_ptr&) = delete;      ^1 error generated.

最佳答案

你的 lock_scope type 声明为包装指向 std::atomic<int>* 的指针,即 std::atomic<int>** . free_lock声明和定义正确地反射(reflect)了这一点。

但随后您尝试初始化 lock_scope带有一个指向 std::atomic<int> 的指针(少一个间接)。

编译器消息相当清楚地说明了这一点:

[...] candidate constructor not viable: 
no known conversion from 'std::atomic *' to 'pointer' (aka 'std::atomic **') [...]

您需要为包装的 lck 添加一个额外的间接寻址或更改 lock_scopefree_lock少用一种间接方式。

关于c++ - 将静态函数删除器与 std::unique_ptr 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com