将对象移动到std::shared_ptr
std::move of an object to a std::shared_ptr
给定以下模板化类:
template<typename Container>
Class A
{
public:
A() : {}
bool push(std::shared_ptr<Container> container)
{
ptr_vec.emplace_back(container)
}
void load(Container c)
{
push(std::make_shared((Container)std::move(c));
}
private:
std::vector<std::shared_ptr<Container>> ptr_vec;
};
以及main.cpp
中的以下代码:
A<std::string> my_A {};
my_A.load("Hello");
我得到以下错误:
错误:对"make_shared(std::__cxx11::basic_string<char>("的调用没有匹配的函数
有人能解释一下这个错误以及如何修复它吗?
给定的代码有很多拼写错误。
但是,如果您修复了这些(此处:https://godbolt.org/z/M81qnh),错误来自std::make_shared
函数,它是一个模板化函数。它需要明确指定的第一个参数
push(std::make_shared<Container>(std::move(c)));
// ^^^^^^^^^^
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理