How To Detect Function Overloads in C++17, std::from_chars Example

The problem: a library function offers several overloads, but depending on the implementation/compiler, some of the overloads are not available. How to check the existence of an overload? And how to provide a safe fallback? In this article, I’ll show you a background “theory” and one case - std::from_chars that exposes full support for numbers or only integer support (in GCC, Clang).

READ MORE...

Simplify code with 'if constexpr' in C++17

Before C++17 we had a few, quite ugly looking, ways to write static if (if that works at compile time) in C++: you could use tag dispatching or SFINAE (for example via std::enable_if). Fortunately, that’s changed, and we can now take benefit of if constexpr! Let’s see how we can use it and replace some std::enable_if code.

READ MORE...

C++17 in details: Templates

For C++17 everyone wanted to have concepts, and as you know, we didn’t get them. But does it mean C++17 doesn’t improve templates/template meta-programming? Far from that! In my opinion, we get excellent features. Read more for details. Intro Do you work a lot with templates and meta-programming? With C++17 we get a few nice improvements: some are quite small, but also there are notable features as well!

READ MORE...

Variadic Templates and a Factory Function

Variadic Templates from C++11 is probably not a feature that you use on a daily basis. But recently, I’ve come across one refactoring example where I’ve decided to give a try and apply variadics. Intro When I was doing some work in some old UI code I’ve noticed several similar lines of code that looked like that:

READ MORE...

SFINAE Followup

As it appears, my last post about SFINAE wasn’t that bad! I got a valuable comments and suggestions from many people. This post gathers that feedback. Comments from @reddit/cpp Using modern approach In one comment, STL (Stephan T. Lavavej) mentioned that the solution I presented in the article was from old Cpp style.

READ MORE...