Search code examples
c++c++11gccgcc4.8constantfolding

Compiler Does No Constant Folding for Masking Class


For an embedded project, I use helper classes for flags and masks. For unknown reasons, the code of the mask class is not correctly constant folded as expected.

A minimal implementation of the mask is shown below:

template<typename Enum, typename MaskValue>
class EnumMask
{
public:
    constexpr inline EnumMask() noexcept : _mask(0) {}
    constexpr inline EnumMask(Enum enumValue) noexcept : _mask(maskFromEnum(enumValue)) {}
    constexpr inline EnumMask(const std::initializer_list<Enum> enumValues) noexcept : _mask(maskFromEnum(enumValues.begin(), enumValues.end())) {}
    constexpr inline operator MaskValue() const noexcept { return _mask; }
private:
    constexpr static inline MaskValue maskFromEnum(const Enum enumValue) noexcept {
        return (static_cast<MaskValue>(1)<<static_cast<uint8_t>(enumValue));
    }
    constexpr static inline MaskValue maskFromEnum(
        typename std::initializer_list<Enum>::const_iterator it,
        typename std::initializer_list<Enum>::const_iterator end) noexcept
    {
        return (it == end ? static_cast<MaskValue>(0) : (maskFromEnum(*it)|maskFromEnum(it+1, end)));
    }
private:
    const MaskValue _mask;
};

The class is used as shown in the following example:

class Driver
{
public:
    enum Pin : uint8_t {
        GPA0 = 0x00,
        GPA1 = 0x01,
        GPA2 = 0x02,
    };
    typedef EnumMask<Pin, uint16_t> PinMask;
    void setPinDirection(const uint16_t mask, bool direction);
    inline void setPinDirection(const PinMask &mask, bool direction) {
        setPinDirection(static_cast<uint16_t>(mask), direction);
    }
};

void main()
{
    Driver d;
    d.setPinDirection({Driver::GPA0, Driver::GPA1}, true);
}

The code is compiled using GCC 4.8.3 with the option -Os. I would expect, the compiler will resolve this code into a single value, yet it actually creates a function to calculate the mask from the values.

Is there a particular reason in my code, which prevents the a proper const folding?


Solution

  • The explanation is actually very simple:

    The begin() and end() members of std::initializer_list are constexpr only as of C++14.

    See reference