Type Erasure, Part 4 -- Andrzej Krzemieński

In part 4, Andrzej wraps up his series on type erasure with a discussion and comparison of facilities in the standard library, Boost, and otherwise.

Type Erasure, Part 4

by Andrzej Krzemieński

From the article:

In this post we will be wrapping up the series on type erasure. We will see an another form of value-semantic type erasure: boost::any, and try to compare the different methods.

Quick Q: Is pass-by-value-and-then-move a bad idiom? -- StackOverflow

Quick A: It's a good style by default when you know you'll keep a copy of the parameter anyway. If you have an expensive-to-move type or otherwise want additional control, you can overload on &/&& or else perfect-forward.

Recently on SO:

Is the pass-by-value-and-then-move construct a bad idiom?

Since we have move semantincs in C++, nowadays it is usual to do

void set_a(A a) { _a = std::move(a); }

The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move.

But what happens if a is an lvalue? It seems there will be a copy construction and then a move assignment (assuming A has a proper move assignment operator). Move assignments can be costly if the object has too many member variables.

On the other hand, if we do

void set_a(const A& a) { _a = a; }

There will be just one copy assignment. Can we say this way is preferred over the pass-by-value idiom if we will pass lvalues?

Quick Q: Why prefer making shared_ptrs via make_shared? -- StackOverflow

Quick A: Because it's more efficient, since it can eliminate an additional allocation.

Recently on SO:

Difference in make_shared and normal shared_ptr in C++

std::shared_ptr<Object> p1 = std::make_shared<Object>("foo");
std::shared_ptr<Object> p2(new Object("foo"));

Many google and stackoverflow posts are there on this, but I am not able to understand why make_shared is more efficient than directly using shared_ptr. Can someone explain me step by step sequence of objects created and operations done by both so that I will be able to understand how make_shared is efficient. I have given one example above for reference.

An overview of smart pointers -- Jens Weller

Jens's latest, following up on his pointers post:

An overview on smart pointers

by Jens Weller

From the article:

So, a smart pointer is only needed, when you use new or other means of dynamic memory allocation. In my opinion, you should prefer to allocate variables on the stack, so when refactoring code (to C++11), you should always ask yourself, if this new is needed, or could be replaced with an object on the stack. When you need to use new, you should always use a smart pointer in my opinion. Also some smart pointers offer a custom deleter, which is handy if you have an object that is either not allocated by new and/or needs to be freed by calling a special function.

N3828: C++ Standards Committee Meeting, Nov 3-8, 2014, Urbana-Champaign

A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.

Document number: N3828

Date: 2014-01-07

C++ Standards Committee Meeting November 3-8, 2014 University of Illinois at Urbana-Champaign

by Jill Peckham

Excerpt:

The Fall 2014 C++ Standards Committee Meeting will be held on the
campus of the University of Illinois at Urbana-Champaign. Hosts for the
meeting are the U of I Department of Computer Science and the Parallel
Computing Institute.

Meetings will be held in the Thomas M. Siebel Center for Computer Science
and the Coordinated Science Laboratory, both conveniently located just a
few blocks from either conference hotel and across the street from each
other.

Registration for C++Now 2014 is now open

Registration for C++Now 2014 is now open.

The Eighth annual C++Now Conference (formerly BoostCon) will be held at the Aspen Center for Physics in Aspen, Colorado, May 12th to 17th, 2014.

Conference website: http://www.cppnow.org

 

Dive into C++11 (#4) — Smart pointers

Hello once more isocpp users, I’m Vittorio Romeo, a computer science student, hobbyist game developer and C++ enthusiast.

I’ve uploaded the fourth episode of “Dive into C++11” on my YouTube channel.

After looking at C and C++'s memory and lifetime management in part 3, we'll take a brief look at C++11 smart pointers. We will learn what they are, what problem they solve, their advantages and their uses.

 

 


The intended audience for this tutorial/screencast are people who have some experience with C++ in general, and who watched the previous episodes. This episode may be very interesting for those with experience with C++ who want to learn more about variable lifetime and memory management.

I greatly appreciate comments and criticism, and ideas for future videos/tutorials.

Feel free to fork/analyze the source code at: https://github.com/SuperV1234/Tutorials

You can find the previous episodes here:

Episode 1
Episode 2
Episode 3
Playlist

Thanks for watching!