C++17: std::byte--Marc Gregoire
Do you know it?
C++17: std::byte
by Marc Gregoire
From the article:
C++17 introduced a new type: std::byte...
March 23-28, London, UK
By Adrien Hamelin | Jun 4, 2018 01:38 PM | Tags: c++17 basics
Do you know it?
C++17: std::byte
by Marc Gregoire
From the article:
C++17 introduced a new type: std::byte...
By Adrien Hamelin | May 10, 2018 11:36 AM | Tags: basics
A reminder.
Using C++17 std::optional
by Bartlomiej Filipek
From the article:
Let’s take a pair of two types <YourType, bool> - what can you do with such composition?
In this article, I’ll describe std:optional - a new helper type added in C++17. It’s a wrapper for your type and a flag that indicates if the value is initialized or not. Let’s see where it can be useful and how you can use it.
By Adrien Hamelin | May 8, 2018 12:29 PM | Tags: basics
Readability is important.
Passing Booleans to an Interface in an Expressive Way
by Jonathan Boccara
From the article:
In order to allow a function to behave in several different way, and to allow its caller to choose amongst these behaviours, we have several tools at our disposal. Plenty, actually.
There are various sorts of polymorphisms embedded in the language such as virtual functions and templates. And we’ve also seen that a caller can specify the desired behaviour explicitly at call site. We’ve seen how to accomplish this by using tag dispatching, and also how to choose between enums and tag dispatching depending on your need.
I now want to top it off with a really simple technique, that will cost you almost nothing, but one that will make your code much more expressive. And even if it’s not rocket science I’m sharing this with you today because I’ve seen many a piece of code that could have benefited from it...
By Jason Turner | Apr 30, 2018 11:08 AM | Tags: embedded basics
Episode 113 of C++ Weekly.
Will It C++? Atari Touch Me From 1978
by Jason Turner
About the show:
In this episode of C++ Weekly Jason discusses the architecture of the handheld Atari game, the "Touch Me" from 1978. Would it be possible to compile a modern C++ program for this 40 year old hardware?
By Marco Arena | Apr 25, 2018 11:34 PM | Tags: c++17 basics
An observation on using std::size & co with statically sized char arrays:
Just be aware of std::size and static C-strings
by Marco Arena
From the article:
C++17 added support for non-member std::size, std::empty and std::data. They are little gems for generic programming...
By Adrien Hamelin | Mar 12, 2018 12:24 PM | Tags: basics
Quick A: All constructors that you define are not trivial.
Recently on SO:
What is a non-trivial constructor in C++?
In simple words a "trivial" special member function literally means a member function that does its job in a very straightforward manner. The "straightforward manner" means different thing for different kinds of special member functions.
For a default constructor and destructor being "trivial" means literally "do nothing at all". For copy-constructor and copy-assignment operator, being "trivial" means literally "be equivalent to simple raw memory copying" (like copy with memcpy).
If you define a constructor yourself, it is considered non-trivial, even if it doesn't do anything, so a trivial constructor must be implicitly defined by the compiler.
In order for a special member function to satisfy the above requirements, the class must have a very simplistic structure, it must not require any hidden initializations when an object is being created or destroyed, or any hidden additional internal manipulations when it is being copied.
For example, if class has virtual functions, it will require some extra hidden initializations when objects of this class are being created (initialize virtual method table and such), so the constructor for this class will not qualify as trivial.
For another example, if a class has virtual base classes, then each object of this class might contain hidden pointers that point to other parts of the very same object. Such a self-referential object cannot be copied by a simple raw memory copy routine (like memcpy). Extra manipulations will be necessary to properly re-initialize the hidden pointers in the copy. For this reason the copy constructor and copy-assignment operator for this class will not qualify as trivial.
For obvious reasons, this requirement is recursive: all subobjects of the class (bases and non-static members) must also have trivial constructors.
By Meeting C++ | Feb 16, 2018 03:24 AM | Tags: meetingcpp intermediate c++17 basics
The first of two talks from Jason Turner from Meeting C++ 2017 is now released:
Practical C++17
by Jason Turner
By Adrien Hamelin | Feb 14, 2018 10:16 PM | Tags: basics
Quick A: Welcome to undefined behaviour.
Recently on SO:
Vector going out of bounds without giving error
STL vectors perform bounds checking when the
.at()member function is called, but do not perform any checks on the[]operator.When out of bounds, the
[]operator produces undefined results.
By Adrien Hamelin | Jan 24, 2018 06:38 PM | Tags: c++17 basics
Small reminder:
C++17: Initializers for if & switch statements
by Marc Gregoire
From the article:
Two small, but very useful C++17 features are initializers for if and switch statements. These can be used to prevent polluting the enclosing scope with variables that should only be scoped to the if and switch statement. The for statement already supports such initializers since the beginning...
By Adrien Hamelin | Jan 24, 2018 06:34 PM | Tags: basics
Quick A: This is not a valid statement.
Recently on SO:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
The statement (4 > y > 1) is parsed as this:
((4 > y) > 1)The comparison operators < and > evaluate left-to-right.
The 4 > y returns either 0 or 1 depending on if it's true or not.
Then the result is compared to 1.
In this case, since 0 or 1 is never more than 1, the whole statement will always return false.