vendredi 6 octobre 2017

Compile error while trying to increment iterator returned by initializer_list.begin()

If the code below is compiled, an error message is produced by clang 4 (live on ideone)

error: expression is not assignable (this one -> ++(init.begin()))

and by GCC 6.3 (live on ideone)

error: lvalue required as increment operand (this one -> ++(init.begin()))

The return value of initializer_list<T>.begin() is const T*, which should be perfectly incrementable. What am I missing here? I am not asking how to make the code work since it is straightforward. What I would like to know why my code is ill-formed.

#include <algorithm>
#include <initializer_list>
using namespace std;

int main() {
    initializer_list<int> init { 1, 2, 3 };
    // omit the first element and do whatever
    for_each(++(init.begin()), init.end(), [](const auto & number){ /* whatever */ });
    return 0;
} 

Aucun commentaire:

Enregistrer un commentaire