Note that there are some explanatory texts on larger screens.

plurals
  1. USPaul Preney
    primarykey
    data
    text
    plurals
    1. This table or related slice is empty.
    1. CO@Martin: I would argue that this answer doesn't get you anywhere. If you know the type you want to create, then you can easily create the shared_ptr --why call a function? Even with the function you need to specify two types: the one before ::create and the template parameter. Ugh as it is not needed! I would think you want to also handle the general case when you DON'T know any types except for the abstract base class --and for such this answer is (respectively) of little use. For example, how do you invoke create() with an iterator to list<shared_ptr<Base>>?
      singulars
    2. COThe linked to thbecker.net page is different than this. On the thbecker.net page X x; is a declared locally within the function --one should simply do return x; (and if RVO does not apply, if it can be moved, the compiler will move it, or it will copy it). With my function you've quoted a is passed in as an rvalue reference. When an rvalue reference (it is not an lvalue reference due to the other overload with T const&) is passed as an argument, it acquires a name and becomes an lvalue --yet it still refers to an rvalue. Thus, return std::move(a); makes a an rvalue again to return it.
      singulars
    3. COYou're welcome! Your question is also very relevant. The new additions (especially variadic templates, move semantics, and perfect forwarding) actually has us all taking steps back to readjust and do things in a simpler way again --more like when we started coding! Perhaps for too long we've all been optimizing to effectively bypass the compiler. With C++11 this is no longer needed as well-thought out/simpler code yields better/optimal performance just by letting the compiler do what it does. So with C++11, I try to remind myself to resist temptation to optimize but to rethink more simply.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload