Virtually sequentially concatenate two C++ std::vectors

Advertisements My question consists of two parts: First part. Am I correct that the following task couldn’t be solved in C++ even with recent innovations in templates? The goal is to virtually (which means no real concatenation should occur) sequentially concatenate two C++ std::vectors of objects of different types for the time of function call.… Read More Virtually sequentially concatenate two C++ std::vectors

Why is my std::priority_queue with custom comparator ordering elements incorrectly?

Advertisements I was refreshing some algorithm knowledge and implementing djikstra in c++ and saw this seemingly broken behavior in std::priority_queue. Can anyone shed some light on why I might be seeing this? I created a priority_queue with a custom comparator, to allow me to insert indices into the priority_queue and prioritize them based on the… Read More Why is my std::priority_queue with custom comparator ordering elements incorrectly?

Why do the new ranges remove_* algorithms *move* the `first` pointer?

Advertisements I was reading through the MSVC STL implementation of std::ranges::remove when I noticed the following line: _First = _RANGES _Find_if_unchecked(_STD move(_First), _Last, _Pred, _Proj); Indeed, cppreference has the following line in their ‘possible implementation’ too: first = ranges::find_if(std::move(first), last, pred, proj); What’s confusing to me is, I’ve just about never seen anyone move an… Read More Why do the new ranges remove_* algorithms *move* the `first` pointer?

How to store a specific object's member function of known signature in a variable in C++?

Advertisements What is the recommended way to store a reference to a non-static member function of a specific signature on some object instance? In a way where the calling code needs not to know of the object’s class (i.e. no casting), just that the function is the correct signature. For example, if there are two… Read More How to store a specific object's member function of known signature in a variable in C++?

Why does "L.insert(it–, i);" behave differently from "L.insert(it, i); it–;"?

Advertisements These are two pieces of code that I ran under the C++11 standard. I expected the post-decrement of the iterator to produce the same effect, but these two pieces of code produce completely different results. Where is my understanding off? list<int> L; int main() { L.push_back(0); L.push_front(1); auto it = L.begin(); for (int i… Read More Why does "L.insert(it–, i);" behave differently from "L.insert(it, i); it–;"?

std::set elements' reference and pointer invariance after insertion

Advertisements I came across a paragraph on cppreference that I don’t think I understand: No iterators or references are invalidated. [If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become… Read More std::set elements' reference and pointer invariance after insertion

Why is shared_ptr implemented using control block and not a static map?

Advertisements To me, it looks like an implementation of std::shared_ptr which stores a reference counter in a static std::unordered_map<void*, struct Counters> would be much more simpler and also allow us to avoid some dirty workarounds like std::enable_shared_from_this (because std::shared_ptr<T>{this} wouldn’t create new control block, only increment a counter for a pointer that already exists in… Read More Why is shared_ptr implemented using control block and not a static map?