GoingNative 2013 C++ Seasoning
Why Stack Overflow recommends it
Stack Overflow users link it when arguing for standard algorithms over hand-written loops, and as the source for std::rotate, stable_partition and type-erasure techniques.
Recommended by 6 people across 6 different months, 2018 to 2022.
What it is
Sean Parent of Adobe gives a GoingNative 2013 talk on writing better C++ by avoiding raw loops, raw synchronization primitives and raw pointers, and by using standard algorithms such as std::rotate and std::stable_partition. It also introduces concept-based runtime polymorphism (type erasure). The video is a re-upload of the conference session.
What people said
Recommends Ali Tavakol · 2019-11 · Stack Overflow · on “std::atomic to what extent?”
In the C++ Seasoning video by Sean Parent [this video] at 33:41 when starting to talk about “no raw synchronization primitives”, he brings an example to show that with raw synchronization primitives we will get it wrong. The example is a bad copy on write class: template <typename T> class bad_cow { struct object_t { explicit object_t(const T& x) : data_m(x) { ++count_m; } atomic<int> count_m; T data_m; }; object_t* object_m; public: explicit bad_cow(const T& x) : object_m(new object_t(x)) { } ~bad_cow() { if (0 == --object_m->count_m) delete object_m; } bad_cow(const bad_cow& x) : object_m(x.object_m) { ++object_m->count_m; } bad_cow& operator=(const T& x) { if (object_m->count_m == 1) { // labe
Recommends einpoklum · 2020-04 · Stack Overflow · on “How to transfer objects from an array of objects to another in C++?”
As @Eljay notes, you are trying to access elements past the end of the arrays. If you were to use a debugger [LINK] to step through your program, you would see this. However, this is also a lesson for you to be careful of writing raw loops, with a lot of "magic-number" indices, and prefer using pre-existing patterns from the libraries (especially std::algorithm) when relevant. See this talk by Sean Parent [this video] about this general principle. Specifically, you could have written your program as follows: #include <range/v3/view.hpp> #include <string> #include <array> #include <iostream> struct card { std::string suite; std::string value; }; std::ostream& operator<<(std::ostream& os, const car
Recommends Bob__ · 2020-06 · Stack Overflow · on “More efficient way to call vector.size()”
Don't fall into premature optimization traps like this. Your compiler is more than capable of optimize those lines if needed (and if instructed). There are other things that you should note. Assuming that vect is a vector , std::vector::size() returns a size_t value, which is an unsigned type. Your compiler may warn you about the comparison i < vect.size() which involves variables of different signedness. If you really want i to be an int (there may be no reasons for it to be unsigned) you can use the following for ( int i = 0, v_size = vect.size(); i < v_size; ++i ) { /* ... */ } Do you really need i at all? Since C++11 you can write range-based for [LINK] loops and, in general, you may wan
Recommends Viktor Latypov · 2020-10 · Stack Overflow · on “How to remove non contiguous elements from a vector in c++”
Here's my attempt on this removal algorithm. Assuming the selection vector is sorted and using some (unavoidable ?) pointer arithmetic, this can be done in one line: template <class T> inline void erase_selected(std::vector<T>& v, const std::vector<int>& selection) { v.resize(std::distance( v.begin(), std::stable_partition(v.begin(), v.end(), [&selection, &v](const T& item) { return !std::binary_search( selection.begin(), selection.end(), static_cast<int>(static_cast<const T*>(&item) - &v[0])); }))); } This is based on an idea of Sean Parent (see this C++ Seasoning [this video] video) to use std::stable_partition ("stable" keeps elements sorted in the output array) to move all selected items to t
Recommends rturrado · 2021-12 · Stack Overflow · on “Erasing the first entry of a vector, after the maximum is reached”
Then you have to see this CppCon 2013 video by Sean Parent's. I think he made `rotate` a bit more famous since then :) [this video]
Recommends Ben · 2022-02 · Stack Overflow · on “C++ How to add new functionality that shares mostly the same code?”
Does this class have any data? If not, consider freeing the functions from the class. (This isn't Java.) In the .cpp you can put as many helper "implementation-detail" functions in an anonymous namespace (`namespace { /* hidden "private" functions go here */ }`). Making a new function (especially a private one) should never be a big decision: It just means you have a few lines of logic that are self-contained. For more on the philosophy of small functions: this classic talk by Sean Parent of Adobe (an architect of Photoshop): [this video]
Cites Dev Null · 2018-08 · Stack Overflow · on “How to detach misplaced element from almost sorted linked list?”
This doesn't directly answer your question as it is currently formulated: What should I change in it ( detach ) to work correctly? This is more of an answer to "How should I change it to make it better". However depending on your goals you might find it useful. The best practice in C++ is to use standard containers and algorithms instead of rolling out your own containers or using raw loops because, among other things, they are very well tested and express your intent clearly for the reader (see a talk by Sean Parent [this video] for more details). Assuming that you have C++11, you could use std::forward_list [LINK] as a singly-linked list implementation and std::adjacent_find [LINK] algorithm to
Cites Ricardo Perl · 2018-09 · Stack Overflow · on “How to allow custom return types of member functions for type erasure in C++?”
Over the last years, type erasure or concept-based runtime polymorphism became quite popular, see e.g. the talks by Sean Parent Better Code: Runtime Polymorphism [LINK] , Inheritance Is The Base Class of Evil [LINK] or C++ Seasoning [this video] resp. the implementations by Adobe [LINK] , Facebook [LINK] , Boost.TypeErasure [LINK] , Boost.te [LINK] or dyno [LINK] . Here is an example from Sean Parent's talk: class object_t { struct concept_t { virtual ~concept_t() = default; virtual void draw_(ostream&, size_t) const = 0; }; template <typename T> struct model final : concept_t { model(T x) : data_(move(x)) { } void draw_(ostream& out, size_t position) const override { draw(data_, out, position);
Cites Jeremy W. Murphy · 2019-03 · Stack Overflow · on “How to efficiently implement bitwise rotate of an arbitrary sequence?”
"The permutation p of n elements defined by an index permutation p(i) = (i + k) mod n is called the k- rotation ." -- Stepanov & McJones [LINK] std::rotate has become a well known algorithm thanks to Sean Parent [this video] , but how to efficiently implement it for an arbitrary sequence of bits? By efficient, I mean minimizes at least two things, i) the number of writes and ii) the worst-case space complexity. That is, the input should be similar to std::rotate but bit-wise specific, I guess like this: A pointer to the memory where the bit sequence starts. Three bit indices: first , middle and last . The type of the pointer could be any unsigned integer, and presumably the larger the better. (Bo
Cites Viktor Latypov · 2020-10 · Stack Overflow · on “Erasing multiple objects from a std::vector?”
While this answer [LINK] by Peter G. in variant one (the swap-and-pop technique) is the fastest when you do not need to preserve the order, here is the unmentioned alternative which maintains the order. With C++17 and C++20 the removal of multiple elements from a vector is possible with standard algorithms. The run time is O(N * Log(N)) due to std::stable_partition . There are no external helper arrays, no excessive copying, everything is done inplace. Code is a "one-liner": template <class T> inline void erase_selected(std::vector<T>& v, const std::vector<int>& selection) { v.resize(std::distance( v.begin(), std::stable_partition(v.begin(), v.end(), [&selection, &v](const T& item) { return