mercredi 21 février 2018

constexpr in for-Statement

provides if constexpr, in which:

the value of condition must be a contextually converted constant expression of type bool. If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded

Is there a way to use this in a for-statement as well? To unroll a loop at compile time? Id like to be able to do something like this:

template <int T>
void foo() {
   for constexpr (auto i = 0; i < T; ++i) cout << i << endl;
}

Aucun commentaire:

Enregistrer un commentaire