Martin Kopecký
Martin Kopecký

Reputation: 1010

enum class varidic parameter pack to expand with binary operator

There is a class template working as enum class flags container:

template<typename E> requires is_enum<E>::value
class flags {
public:
    flags( E value ) noexcept : m_value( to_underlying( value ) ) {}

    flags operator|( const E value ) noexcept {
        m_value |= to_underlying( value );
    }

    flags & operator|=( const E value ) noexcept {
        m_value |= to_underlying( value );
        return *this;
    }

    flags & operator=( const E value ) noexcept {
        m_value = to_underlying( value );
    }

    template<E FLAG>
    bool is_set( void ) noexcept {
        return ( m_value & to_underlying( FLAG ) );
    }

private:
    /* Abstracted type to store 'combined' value */
    typename underlying_type<E>::type m_value;
};

template<typename E> requires is_enum<E>::value
flags<E> operator|( E lhs, E rhs ) noexcept {
    return ( flags( lhs ) | rhs );
}

and some sample flags:

enum class mode { MODE_0 = 1, MODE_1 = 2, MODE_2 = 4 };

and finally to use it like:

template<mode... MODES>
struct user {
   void apply( flags<mode> m = MODES | ... ) {} // <- this does not work
};

user<mode::MODE_0, mode::MODE_2> sample; 

uses void apply( flags<mode> m ) where m contains the internal m_value equal to mode::MODE0 | mode::MODE2 ( m_value == 5 )

So the question is:

The goal is to take the variadic parameter pack containing the particular modes, combine them with operator| to create single flags<mode> instance...

so how to fix this line to make it work?

void apply( flags<mode> m = MODES | ... ) {} // <- this does not work

Upvotes: 0

Views: 447

Answers (1)

cigien
cigien

Reputation: 60218

Your syntax for the fold expression is incorrect; the fold expression needs to be in parentheses:

void apply( flags<mode> m = ( MODES | ... ) ) {}
                         // ^_____________^

Upvotes: 1

Related Questions