vendredi 14 juin 2019

How to avoid code duplicate const and non-const collection processing with lambda

The answer here does not work for this pattern in C++17:

template <typename Processor>
void Collection::ProcessCollection(Processor & processor) const
{
    for( int idx = -1 ; ++idx < m_LocalLimit ; )
    {
        if ( m_Data[ idx ] )
        {
            processor( m_Data[idx] );
        }
    }

    const int overflowSize = OverflowSize();

    for( int idx = -1 ; ++idx < overflowSize ; )
    {
        processor( (*m_Overflow)[ idx ] );
    }
}

// How to avoid this repetition for non-const version?
template <typename Processor>
void Collection::ProcessCollection(Processor & processor)
{
    for( int idx = -1 ; ++idx < m_LocalLimit ; )
    {
        if ( m_Data[ idx ] )
        {
            processor( m_Data[idx] );
        }
    }

    const int overflowSize = OverflowSize();

    for( int idx = -1 ; ++idx < overflowSize ; )
    {
        processor( (*m_Overflow)[ idx ] );
    }
}

Due to the argument passed to the lambda Processor being const and not matching its signature.

Aucun commentaire:

Enregistrer un commentaire