简体   繁体   中英

C++ range based for loop over vector of pointers. How to capture elements by const reference?

How can I capture the elements by const reference in a range-based for loop over a vector containing pointers?

#include <iostream>
#include <memory>
#include <vector>

class Foo {
public:
    Foo(int a) : a{a} {}
    int value() const { return a; }
private:
    int a;
};

int main() {
    std::unique_ptr<Foo> f1 = std::make_unique<Foo>(1);
    std::unique_ptr<Foo> f2 = std::make_unique<Foo>(2);
    std::vector<Foo *> v;
    v.push_back(f1.get());
    v.push_back(f2.get());
    //for (const Foo &f : v) {  // <-- This does not work..
    //    std::cout << f.value() << '\n';
    //}
    for (Foo *f_ptr : v) {
        const Foo &f = *f_ptr;  // <-- I would like to avoid this
        std::cout << f.value() << '\n';
    }

    return 0;
}

I would like to avoid creating a separate variable just to construct the reference:

    for (Foo *f_ptr : v) {
        const Foo &f = *f_ptr;  // <-- I would like to avoid this
        // ...
    }

Is it possible to have f assigned directly in for loop condition? Eg something like:

for (const Foo &f ...) { }

The vector contains pointers, so that's what you get. If you want something else you need to loop over a different object.

C++20 ranges offers a way to do that. It wouldn't be very hard to roll your own variant.

#include <iostream>
#include <memory>
#include <vector>
#include <ranges>

class Foo {
public:
    Foo(int a) : a{a} {}
    int value() const { return a; }
private:
    int a;
};

int main() {
    std::unique_ptr<Foo> f1 = std::make_unique<Foo>(1);
    std::unique_ptr<Foo> f2 = std::make_unique<Foo>(2);
    std::vector<Foo *> v;
    v.push_back(f1.get());
    v.push_back(f2.get());

    for (const auto& f : std::ranges::views::transform(v, [](auto ptr) -> const Foo& { return *ptr; })) {
        std::cout << f.value() << '\n';
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM