Articles contenant le tag C++

Clamping values with style

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); […]

Pas de commentaire

Proper handling of temporary objects

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 […]

Pas de commentaire

The Hidden Dangers of std::function and lambdas

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 […]

Pas de commentaire

boost goodie to clamp numbers

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)

,

Pas de commentaire

Namespace, template typedef and operator overloads – The Evil trio

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 { […]

Pas de commentaire

Correctly handling attached files with Poco::Net::HTMLForm

I recently had to develop an visualisation application. To make it easier to deploy, I chose to make it a C++ application with embedded web server. In that application, there is a need for the user to submit some data to be analyzed. Users have the choice to submit text directly (filling in a textarea) […]

, , , ,

Pas de commentaire

Moving an object that wraps a vector and an iterator on that vector

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())     { } […]

, ,

Pas de commentaire

C++ Thread synchronization pitfall: using a barrier to synchronize a thread start

The context: You want to create a worker thread. Before the main thread goes on, you want to ensure that the worker thread starts. Using a boost::barrier seems like a good idea.

,

Pas de commentaire

std::unique_ptr, virtual and missing virtual destructor = major pitfall

In our company, the build infrastructure runs unit tests in a valgrind shell, trying to detect memory leaks at the time the unit tests are executed. And every now and then, although our memory allocations are mostly handled through std::unique_ptr or std::shared_ptr, a leak pops up on the radar. And usually with the most useless […]

, ,

Pas de commentaire

Why does std::for_each return the Functor ?

In short: Because the provided Functor is copied by for_each. A bit longer: the functor has to be copied so that for_each can use a reference to it in the loopage as the functor is copied, the context of the functor would be lost at end of for_each if it was not returned class Object […]

,

Un commentaire