Articles contenant le tag C++
Mid-point and linear interpolation done right
Posté par Olivier dans Trucs & Astuces le 22 mai 2025
How many times did we write code like: Although this code is correct, it may suffer from overflows. The typical way to avoid those is to cast to a wider type. But how can you do this when the start and stop values are already using the largest integer type (eg. int64_t) ? The C++ […]
The hidden cost of std::ranges
Posté par Olivier dans Trucs & Astuces le 22 mai 2025
At work, we have recently adopted some commercial static checker to hunt bugs in our codebase. The tool does not only identify bugs and security vulnerabilities. It is also advising you on opportunities to modernize your code to use the latest and greatest of the C++ language and of its standard library. One of the […]
Overflow-safe integer mul&div
Posté par Olivier dans Trucs & Astuces le 22 décembre 2023
When you do a multiply & divide operation, to ensure you don’t loose precision, you would start by the multiplication before the division. But in that case, it could be that the result of the multiplication overflows. The natural solution would go to use an intermediate storage of larger size. This uses concepts (C++20). For […]
Clamping values with style
Posté par Olivier dans Trucs & Astuces le 12 mai 2021
The following question was raised in one of our software devs channel at work: On style and readability and intent : std::min or std::clamp or explicit if/reassign. Option 1: int x = someValue;if (x > max) { x = max;} Option 2: int x = std::min(someValue, max); Option 3: int x = std::clamp(someValue, 0, max); […]
Proper handling of temporary objects
Posté par Olivier dans Trucs & Astuces le 10 mars 2021
Purpose Making a wrapper that keeps reference to some constructor arguments.That’s OK as long as the wrapper lifetime is longer or equal to its construction arguments. If the wrapper is created from temporary objects, it should better be used in the same line _or_ it will make reference to possibly destroyed objects. Example Available on […]
The Hidden Dangers of std::function and lambdas
Posté par Olivier dans Trucs & Astuces le 30 septembre 2019
Once upon a time, there was an active object which had callbacks using std::function.On the same day, a user of this object registered a callback function which, in its core, cancelled the registration. That callback was a lambda that aside of that took some reference in the current context.And guess what happened ? Here is […]
boost goodie to clamp numbers
Posté par Olivier dans Trucs & Astuces le 10 juin 2015
How do you clamp a number in a given range in C++ ? Classical way to do it is: double clamped = std::min(maxValue, std::max(minValue, value)) But why repeat this magic formula when boost has clamp() for you ? From now on, do: #include double clamped = boost::algorithm::clamp(value, minValue, maxValue)
Namespace, template typedef and operator overloads – The Evil trio
Posté par Olivier dans Trucs & Astuces le 10 juin 2015
What an unconfortable situation ? I had the following code which would not compile. // Range.h #include <boost/numeric/interval.hpp> #include <iostream> namespace App { template <typename T> using Range = boost::numeric::interval<T>; template <typename T> std::ostream& operator<<(std::ostream &os, const Range<T> &r) { … } } // namespace App // Filter.cpp #include "Range.h" #include <iostream> namespace App { […]
Moving an object that wraps a vector and an iterator on that vector
Posté par Olivier dans Trucs & Astuces le 27 août 2014
I recently stumbled upon code that looked very suspicious to me. Basically, it was a class which amongst other things held a vector and some iterators from that vector. Something like this: struct Wrapper { typedef std::vector<int> Data; Data data; Data::iterator active; Wrapper() : data(), active(data.end()) { } […]