What is the reason behind cbegin/cend?

It’s quite simple. Say I have a vector: std::vector<int> vec; I fill it with some data. Then I want to get some iterators to it. Maybe pass them around. Maybe to std::for_each: std::for_each(vec.begin(), vec.end(), SomeFunctor()); In C++03, SomeFunctor was free to be able to modify the parameter it gets. Sure, SomeFunctor could take its parameter … Read more

Sell me const-correctness

This is the definitive article on “const correctness”: https://isocpp.org/wiki/faq/const-correctness. In a nutshell, using const is good practice because… It protects you from accidentally changing variables that aren’t intended be changed, It protects you from making accidental variable assignments. For instance, you are protected from if( x = y ) // whoops, meant if( x == … Read more

“const correctness” in C#

I’ve come across this issue a lot of times too and ended up using interfaces. I think it’s important to drop the idea that C# is any form, or even an evolution of C++. They’re two different languages that share almost the same syntax. I usually express ‘const correctness’ in C# by defining a read-only … Read more

What are the use cases for having a function return by const value for non-builtin type?

Basically, there’s a slight language problem here. std::string func() { return “hai”; } func().push_back(‘c’); // Perfectly valid, yet non-sensical Returning const rvalues is an attempt to prevent such behaviour. However, in reality, it does way more harm than good, because now that rvalue references are here, you’re just going to prevent move semantics, which sucks, … Read more

tech