Automatically Made Weak -- Brett Hall

A nice nugget about a handy make_weak one-liner:

Automatically Made Weak

by Brett Hall

From the article:

I’ve been experimenting with the new “auto everything” style in C++. I also make regular use of std::weak_ptr... [and wouldn't it be nice if] we can just type

auto weak = make_weak(shared);

Add a Comment

Comments are closed.

Comments (3)

0 0

NoSenseEtAl said on Nov 15, 2013 05:41 PM:

in my humble programming experience more often I need to try to get shared_ptr from weak, so maybe auto sp = try_shared(wp) would be nice
1 0

H Sutter said on Nov 18, 2013 06:46 AM:

@NoSenseEtAl: You have it in the standard -- just write auto sp = wp.lock(); . The standard provides that one because it is the common direction. The reason this author had to make his own make_weak convenience function for type deduction is that there is no equivalent member function (or type-deducing free function) on shared_ptr that goes in the reverse direction.
0 0

NoSenseEtAl said on Nov 18, 2013 06:59 AM:

@Herb
Doh. smile Thank you...
P.S. comments are not working on your site.
Your comment is awaiting moderation. //For days