让我们有类Foo和方法void use_weak_ptr(std::weak_ptr<Foo>)。有没有办法确保(最好是在编译时)该方法不会被临时调用?
允许这样做:
auto shared = std::make_shared<Foo>();
use_weak_ptr(shared);
Run Code Online (Sandbox Code Playgroud)
不允许这样做:
use_weak_ptr(std::make_shared<Foo>());
Run Code Online (Sandbox Code Playgroud)
编辑:Godbolt的建议。
我编写了一个库,它公开了对几个相关对象类型的引用.所有这些对象的生命周期都由库内部管理boost::shared_ptr
根据库的性质,库的用户还能够知道任何暴露对象的寿命.因此,他们可以存储指针或保持对这些对象的引用.他们这样做是合理的,并知道这些对象何时不再有效.
但我感到内疚,迫使我的用户合理.
让库暴露weak_ptr给它的对象是否可以接受?还有其他图书馆吗?
我已经在应用程序中描述了这个库的用法,并且发现它对于完全暴露而言过于关键任务weak_ptr.
让匹配的API函数暴露引用或 weak_ptr或使任何对象能够暴露weak_ptr自身是否更明智?
以下是weak_ptr的构造函数中的两个:http: //msdn.microsoft.com/en-us/library/bb982126.aspx
weak_ptr(const weak_ptr&);
template<class Other>
weak_ptr(const weak_ptr<Other>&);
Run Code Online (Sandbox Code Playgroud)
实际代码(来自memory):
weak_ptr(const weak_ptr& _Other)
{ // construct weak_ptr object for resource pointed to by _Other
this->_Resetw(_Other);
}
template<class _Ty2>
weak_ptr(const weak_ptr<_Ty2>& _Other,
typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
void *>::type * = 0)
{ // construct weak_ptr object for resource pointed to by _Other
this->_Resetw(_Other);
}
Run Code Online (Sandbox Code Playgroud)
Q1:为什么顶级拷贝构造函数就在那里?它看起来像每个案例的底部(包括前一个).它甚至被调用了吗?如果它们没有包含它,那么底部的它会占据它的位置吗?
Q2:底层(模板化)构造函数的第二个参数发生了什么.我想我理解SFINAE方面,但我不明白为什么有一个额外的*之后::type
我仍然是新stl成员的新手.任何人都可以指出为什么这段代码会给出分段错误?
#include<memory>
#include<stdio.h>
#include<map>
#include<set>
#include<string>
using namespace std;
struct StubClass
{
weak_ptr<string> b;
int c;
friend bool operator==(StubClass x,StubClass y);
friend bool operator<(StubClass x,StubClass y);
StubClass(weak_ptr<string> x):b(x){c=5;}
};
bool operator==(StubClass d,StubClass c) { return d.b==c.b;}
bool operator<(StubClass d,StubClass c) { return d.b<c.b; }
int main()
{
shared_ptr<string> spPtr(new string("Hello"));
weak_ptr<string> wpPtr(spPtr);
StubClass hello(wpPtr);
set<StubClass> helloSet;
helloSet.insert(hello);
if(helloSet.find(StubClass(wpPtr))!=helloSet.end()) printf("YAYA");
else puts("Bye");
}
Run Code Online (Sandbox Code Playgroud)
错误符合
if(helloSet.find(StubClass(wpPtr))!= helloSet.end())printf("YAYA");
更多的研究表明,调用StubClass的比较器函数时会出现问题.我在这里编译程序
编辑:
bool operator==(StubClass d,StubClass c) { return d.b.lock()==c.b.lock();}
bool operator<(StubClass d,StubClass c) …Run Code Online (Sandbox Code Playgroud) 我有一个SpriteManager类,为我加载和缓存精灵,并从缓存中删除未使用的精灵.无论如何,这是个主意,我有点卡住了.我有一个map<string,weak_ptr<ALLEGRO_BITMAP>>存储sprite的地方,并使用weak_ptr来生成shared_ptr.现在我正在尝试使用也从地图中删除位图的删除器,它看起来像这样(显然不起作用):
[&bitmaps](ALLEGRO_BITMAP* bmp){
for(auto it = bitmaps.begin(); it!=bitmaps.end(); ++it) {
if((*it).second == bmp) {
bitmaps.erase(it);
al_destroy_bitmap(bmp);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
位图是我正在谈论的地图.当然,我不能比较(*it).second和bmp,但我也因为我在缺失者是无法锁定的weak_ptr.除了保持弱指针和原始指针外,我真的别无选择吗?
我正在阅读这里的boost DateTime库,其中包含许多示例,例如:
time_zone_ptr zone(new posix_time_zone("MST-07"));
Run Code Online (Sandbox Code Playgroud)
我很好奇为什么使用关键字'new'不会导致内存泄漏?我调查了boost源代码,并注意到它有两个不同版本的构造函数,一个使用shared_ptr,另一个使用weak_ptr.有人可以解释一下这些是如何工作的,为什么上面这行可以安全写?
有效的Modern C++(第136页)使用以下示例进行激励std::weak_ptr.缓存被定义为无序映射,其中指向对象的弱指针作为值.每当此缓存的客户端(通过密钥)请求对象时,将查找相应的弱指针并lock()在其上调用.如果结果std::shared_ptr不是null,则返回.否则,从外部数据库重新加载对象,输入到缓存中并std::shared_ptr返回该对象.
现在的问题是:有人可能认为可以在没有的情况下实现它std::weak_ptr,而是将强大的共享指针存储为缓存值.如果use_count()强指针等于1,则意味着所有客户端的指针都已被销毁.这个例子的重点是使用std::weak_ptr允许我们通过实际删除对象来节省内存吗?
我想了解weak_ptr已过期的地图中的条目(类型为boost :: weak_ptr)会发生什么.地图中的相应条目是否会自动删除?
键是整数,对应的值是weak_ptr.
我编写的示例代码,但无法编译
#include <iostream>
#include <map>
#include <boost/enable_shared_from_this.hpp>
using namespace std;
class Foo : public boost::enable_shared_from_this<Foo> {
public:
Foo(int n = 0) : bar(n) {
std::cout << "Foo: constructor, bar = " << bar << '\n';
}
~Foo() {
std::cout << "Foo: destructor, bar = " << bar << '\n';
}
int getBar() const { return bar; }
boost::shared_ptr<Foo> inc_ref() {
return shared_from_this();
}
private:
int bar;
};
std::map<int, boost::weak_ptr<Foo> > mappy;
int main()
{
boost::shared_ptr<Foo> sptr(new …Run Code Online (Sandbox Code Playgroud) 根据std::weak_ptr文档,可以构建一个constexpr weak_ptr:
#include <memory>
constexpr weak_ptr<int> foo{};
Run Code Online (Sandbox Code Playgroud)
但是,使用clang尝试这个会产生一个编译错误,抱怨constexpr变量不能有非文字类型' const std::weak_ptr<int>',这是因为weak_ptr<int>有一个用户提供的析构函数.(确实如此,查看libc ++标头)
我的问题是,这是一个libc ++错误,还是constexpr weak_ptr没有意义,并且constexpr默认构造函数是错误的?我可以期待这种情况在未来发挥作用吗?
我是智能指针的新手,我试图绕过我的脑袋,为什么在取消引用运算符之后weak_ptr会过期.我以前测试的代码在这里:
#include <memory>
#include <iostream>
#include <vector>
using namespace std;
struct node
{
weak_ptr<node> parent;
shared_ptr<node> child;
int val;
};
shared_ptr<node> foo()
{
shared_ptr<node> a = make_shared<node>();
shared_ptr<node> b = make_shared<node>();
a->val = 30;
b->val = 20;
b->parent = a;
a->child = b;
return a;
}
int main()
{
shared_ptr<node> c = foo();
node d = *foo();
if (c->child->parent.expired())
{
cout << "weak ptr in c has expired." << endl;
}
if (d.child->parent.expired())
{
cout << "weak ptr in d …Run Code Online (Sandbox Code Playgroud) c++ ×10
weak-ptr ×10
c++11 ×5
shared-ptr ×3
boost ×2
c++-concepts ×1
c++20 ×1
constexpr ×1
datetime ×1
destructor ×1
stl ×1
timezone ×1
type-traits ×1