将资源移出泛型类
Moving resources out of a generic class
在上下文中如下:
template <class T>
struct MyStruct
{
T resource;
decltype(auto) getResource()
{
return std::move(resource);
}
};
我的getResource
方法是否在做我希望它做的事情,即将resource
成员移出类?我想在不再使用MyStruct
并且可以从中"窃取"内存的情况下使用它。
template <class T>
struct MyStruct
{
T resource;
decltype(auto) getResource()
{
return std::move(resource);
}
};
decltype(auto)
是T&&
的,但T&&
本身不会窃取资源(但允许它被隐式窃取)。
另一种选择是
template <class T>
struct MyStruct
{
T resource;
T takeResource()
{
return std::move(resource);
}
};
在这里,一旦调用takeResource
,资源就已转移。
所以例如
MyStruct<std::unique_ptr<int>> foo; foo.resource = std::make_unique<int>(42);
*foo.get_resource() = 51; // No transfer ownership, just get a reference
*foo.get_resource() = 69; // still works.
但
*foo.get_resource() = 51; // Transfer ownership, and resource will be released here
*foo.get_resource() = 69; // dereferencing nullptr -> UB.
这完全取决于T
是什么以及它的移动构造函数是如何编写的。例如,如果它是一个int
,它只是复制它。如果是std::unique_ptr
,它将完全按照您的期望进行操作。
为了更好地理解,请参阅此示例:
#include <iostream>
#include <string>
class copyable{
public:
copyable():x(0){};
~copyable()=default;
copyable(copyable const&)=default;
copyable(copyable&&)=default;
int x;
};
class moveable{
public:
moveable():x(new int{0}){};
~moveable(){
delete[] x;
};
moveable(moveable const&)=delete;
moveable(moveable&& other){
this->x=other.x;
other.x=nullptr;
};
int* x;
};
template <class T>
struct MyStruct
{
T resource;
decltype(auto) getResource()
{
return std::move(resource);
}
};
int main()
{
MyStruct<copyable> a;
std::cout << a.resource.x <<"n";
MyStruct<moveable> b;
std::cout << "address: "<< b.resource.x << "tval: " << *b.resource.x <<"nn";
auto temp_a=a.getResource();
std::cout << temp_a.x <<"n";
auto temp_b=b.getResource();
std::cout << "address: "<< temp_b.x << "tval: " << *temp_b.x <<"nn";
std::cout << a.resource.x <<"n";
std::cout << "address: "<< b.resource.x << "tval: " << /* *b.resource.x << THIS IS UB */ "nn";
}
输出:
0 address: 0x2d366f0 val: 0 0 address: 0x2d366f0 val: 0 0 address: 0 val:
现场演示
std::move
不会自行移动,这是一个轻微的误称。 std::move here只能确保右值。您的 T 型还需要有一个构造函数来允许实际移动它。
总而言之,您无法确保您的资源不会被复制。
如果要明确谁在特定时间拥有唯一资源,请传递唯一指针。 唯一指针是通用的,如果您优化设计足够长的时间,则可能是您最终实现的东西。
相关文章:
- C++泛型类错误,问题出在哪里?
- C++泛型类,单独实现?
- 如何在 C++ 中声明泛型类
- 在C++中分配泛型类
- 基于变量输入的泛型类方法计算
- C++处理取消引用语法的泛型类
- 用于重载的泛型类成员函数
- 将类型作为指向另一个类的对象的指针的泛型类-NOT WORKING
- 在泛型类中使用带有 CUDA 关键字的模板类
- 在类中使用泛型类成员
- 如何在main中重载运算符>泛型类.cpp
- C++泛型类中的字段/属性
- 用返回子类的方法设计C++泛型类
- C++ 堆栈泛型类,int 变量将 32765 作为值而不是 1
- 将资源移出泛型类
- C++中的泛型类
- 可以在泛型类中为一种类型提供一个可用的函数
- 如何在C++中从泛型类创建对象
- 用泛型类指针作为参数声明函数指针的正确语法是什么
- 使用具有结构成员的泛型类时发生c++编译器错误