jeudi 21 juin 2018

range-based for loop over references

This is question is out of curiosity, not necessity. One way I have found C++11's range based for loop useful is for iterating over discrete objects:

#include <iostream>
#include <functional>

int main()
{
    int a = 1;
    int b = 2;
    int c = 3;

    // handy:
    for (const int& n : {a, b, c}) {
        std::cout << n << '\n';
    }

I would like to be able to use the same loop style to modify non-const references too, but I believe it is not allowed by the standard (see Why are arrays of references illegal?):

    // would be handy but not allowed:
    // for (int& n : {a, b, c}) {
    //     n = 0;
    // }

I thought of two workarounds but these seem like they could incur some minor additional cost and they just don't look as clean:

    // meh:
    for (int* n : {&a, &b, &c}) {
        *n = 0;
    }

    // meh:
    using intRef = std::reference_wrapper<int>;
    for (int& n : {intRef (a), intRef (b), intRef (c)}) {
        n = 0;
    }
}

So the question is, is there a cleaner or better way? There may be no answer to this but I'm always impressed with the clever ideas people have on stackoverflow so I thought I would ask.

Aucun commentaire:

Enregistrer un commentaire