Ist es möglich, statische for-Schleife in C ++ zu entwickeln?

Ist es möglich, dass so etwas existiert?

template<int Channel>
void deduce_mask(Matrix const &src, int mask[])
{
    //I hope i could become a constant and the compiler would unroll the loop at compile time        
    for(int i = Channel; i != -1; --i)
    {            
        //mapper is a helper class which translate two and three dimension into one dimension index
        //constexpr makes it possible to find out the index at compile time
        mask[mapper(0, 1, i)] = src(row - 1, col)[i];
        mask[mapper(1, 1, i)] = src(row, col)[i];
        mask[mapper(2, 1, i)] = src(row + 1, col)[i];    
    }
}

anstatt

template<int Channel>
class deduceMask
{
public:
    static void deduce_mask(matrix const &src, int mask[]);
};

template<int Channel>
void deduce_mask(matrix const &src, int mask[])
{                
    mask[mapper(0, 1, Channel)] = src(row - 1, col)[Channel];
    mask[mapper(1, 1, Channel)] = src(row, col)[Channel];
    mask[mapper(2, 1, Channel)] = src(row + 1, col)[Channel];    

    deduceMask<Channel - 1>::deduce_mask(src, mask);
}

template<>
class deduceMask<-1>
{
public:
    static void deduce_mask(matrix const &src, int mask[])
    {

    }
};

Die zweite Lösung ist die einzige Lösung, die ich finden könnte, wenn der Compiler das Ergebnis zur Kompilierungszeit herausfinden soll. Habe ich eine einfache Möglichkeit, das "i" zu einem konstanten Wert wie bei der Metaprogrammierlösung zu machen? Für mich ist es viel einfacher, mit einer einfachen for-Schleife zu arbeiten, als mit der Metaprogramm-Version.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage