NEST main@caf0ae8
 
Loading...
Searching...
No Matches
nest::EnableBitMaskOperators< Enum > Struct Template Reference

These methods support type-safe bitfields using C++'s enum classes. More...

#include <enum_bitfield.h>

Static Public Attributes

static const bool enable = false
 

Detailed Description

template<typename Enum>
struct nest::EnableBitMaskOperators< Enum >

These methods support type-safe bitfields using C++'s enum classes.

Define the bitfield flags as follows:

.. code-block:: C++

enum class MyFlags : unsigned { FIRST_FLAG = 1 << 0, SECOND_FLAG = 1 << 1, THIRD_FLAG = 1 << 2, FOURTH_FLAG = 1 << 3 };

To prevent template substitution from enabling bitfield operators on every enum class, we use the enable_if mechanism (see https://en.cppreference.com/w/cpp/language/sfinae).

A templated struct EnableBitMaskOperators is defined, that, regardless of the template type, contains a single static constant member enable, set to false. This is the "default", as the template may be specialised with any type. However, we can override this behaviour by explicitly providing a template specialisation of EnableBitMaskOperators for a particular type–namely, our bitfield enum class MyFlags–with a static constant member by the same name but with value set to true. If we then want to ask during function definition of bitmask operations whether they should be defined for an arbitrary type T, all we have to do is specalise EnableBitMaskOperators by this type T, and check the value of its enable member using enable_if.

To enable the bitfield operators for our MyFlags class, we thus specialise the template:

.. code-block:: C++

template <> struct EnableBitMaskOperators< MyFlags > { static const bool enable = true; };

Finally, we can instantiate a bitfield and do some operations on it, for example:

.. code-block:: C++

MyFlags my_flags = MyFlags::FIRST_FLAG | MyFlags::FOURTH_FLAG; my_flags |= MyFlags::THIRD_FLAG; if ( flag_is_set( my_flags, MyFlags::FOURTH_FLAG ) ) { std::cout << "Fourth flag is set!" << std::endl; }

Member Data Documentation

◆ enable

template<typename Enum >
const bool nest::EnableBitMaskOperators< Enum >::enable = false
static

The documentation for this struct was generated from the following file: