Skip to content

The tuple

The addition of variadic templates allows an arbitrary-types tuple class to be added to C++. The tuple is essentially an unnamed struct with positional arguments — its elements are accessed through their position rather than by a name. It is a generalised version of the pair class.

Firstly, it is defined in the header <tuple>. To define a tuple, simply use std::tuple with template arguments being the types that you want to be contained in the tuple.

For example, to declare a tuple whose three fields are char, int and string, you would write

typedef std::tuple<char, int, std::string> MyTuple;

Read more…

Advertisements

Code on github

Code that I post here is available on the github repository https://github.com/jarro2783/thenewcpp

You can clone it with

git clone https://github.com/jarro2783/thenewcpp.git

There are standalone examples and actual code like the Variant. Any actual code is in the directory “juice”, and in the namespace Juice. Any new code I make will be added to the namespace Juice to build up the Juice C++11 project. It’s a play on Boost and the Boost Juice brand, and the project’s tagline is Juice up your C++11.

Improving the variant class

Now that alignment support has been implemented in GCC, I can improve the Variant class. At the same time I will discuss a few other improvements that I have made.

Read more…

Generalised attributes

GCC has recently implemented generalised attributes. In this post I will discuss what they are and how they can be useful.

Generalised attributes are an extendible means to specify compiler specific attributes for particular language constructs. Previously these have had compiler specific syntax, and as such, wouldn’t even compile with the wrong compiler. The GNU compiler uses __attribute__, and the Microsoft compiler uses the __declspec keyword.

Attributes are declared by placing an attribute specifier in a declaration. An attribute specifier is said to appertain to a particular entity in the declaration. In other words, the specifier applies to some part of the declaration. The specifier is either an alignas specification, or a list of attributes inside double square brackets ([[]]).

Read more…

Alignment support

GCC has recently implemented alignment support. In this post I will discuss what this is and how you can use it.

There are four additions that are related to alignment support, these are the alignas alignment-specifier, the alignof expression, alignment arithmetic library support and std::align for aligning pointers. I will explain these all individually below.

Alignment is the requirement that the memory address of the first byte of certain types of objects be modulo zero with respect to some integer. For example, int32_t has an alignment of 4, it can only be placed in memory locations divisible by 4.

Alignments are ordered from weaker to stronger or stricter. An alignment that is stricter than another has a larger value. It is said to be stricter because there are less memory locations at which it can be placed, therefore the compiler is stricter in where that object can be placed.

Read more…

TransLucid 0.2 released

TransLucid version 0.2 has been released. With this release comes the ability to replace and delete declarations, intensions as first-class values, and lazy parsing and processing of declarations. It can be downloaded from Sourceforge at http://sourceforge.net/projects/translucid.

You can read all about the latest features at cartesianprogramming.com.

shared pointers

Shared pointers allow you to hold on to several copies of the same pointer, without having to keep explicit track of membership. Rather than one object owning the pointer, a shared pointer counts the number of references there are to the actual pointer, and deletes the object when that counter reaches zero.

The declaration of the shared pointer class is:

template <class T>
class shared_ptr;

The shared pointer class is a shared pointer to something—it has a strong type just like any other pointer.

The most basic way to use the shared pointer is to create one, and then pass it around. The simplest constructor is:

template <class Y> explicit shared_ptr(Y* p);

You use this by simply writing something like:

shared_ptr<int> x(new int(5));

Note that the constructor is explicit because you don’t want to accidentally create a shared pointer by passing it to a function without realising. If it wasn’t explicit, you could pass a pointer to a function that expects as argument a shared pointer, and that would convert it to the shared pointer and suddenly your pointer that you don’t want to be managed might get deleted when you don’t want it to be.

It is preferable to use std::make_shared, which creates a shared pointer and allocates both the memory for the object and the pointer’s data block with only one call to new. The example above would be better written as:

auto x = std::make_shared<int>(5);

Read more…