Thread: Range-based looping over container of references

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    66

    Range-based looping over container of references

    Hey guys,

    I've got a simple question: Is it possible to create a class that stores (non-const) references to some objects and enables users direct access by using range-based for loops on them?

    Code:
    class container {
    public:
        void add(int& value);
        void remove(int& value);
        ...
    };
    
    int main()
    {
        container c;
        for (auto& value:c) {
            // `value' should be accessible as type `int&' instead of being a pointer, `std::reference_wrapper<int>' or something like that
        }
    }
    I think the code shown above provides sufficient explanation of what I want.. Thanks in advance!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Yes, you just need to implement a bunch of iterators.

    More specifically, these:
    C++ concepts: Container - cppreference.com

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Argh, I already was afraid there would be no other way.. Implementing tons of iterators is actually not what I want :/ Thanks anyway!

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Just implementing begin and end properly should 'work', at this moment.

    In C++17, there will probably be a lot of functionalty that depend on your class having some functions.
    Last edited by manasij7479; 03-28-2015 at 02:03 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by milli-961227
    I've got a simple question: Is it possible to create a class that stores (non-const) references to some objects and enables users direct access by using range-based for loops on them?
    I think that you will have a problem creating a class that stores references such that they can be added and removed. You can have members of reference type or a reference to a pointer, but not a container of references or a pointer to a reference. However, if you have a container of (smart) pointers, you could implement what manasij7479 mentioned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    I think that you will have a problem creating a class that stores references such that they can be added and removed. You can have members of reference type or a reference to a pointer, but not a container of references or a pointer to a reference. However, if you have a container of (smart) pointers, you could implement what manasij7479 mentioned.
    Yes, that's what I'm trying to achive:

    Code:
    class container {
    public:
        using iterator=typename std::vector<std::reference_wrapper<argument>>::iterator; // won't work
        using const_iterator=typename std::vector<std::reference_wrapper<argument>>::const_iterator; // won't work
        
        iterator begin() noexcept; // calling `*begin()' would resolve into `std::reference_wrapper<argument>' instead of `argument'
        const_iterator begin() const noexcept;
    
        iterator end() noexcept; // same as `begin()'
        const_iterator end() const noexcept;
        
    private:
        std::vector<std::reference_wrapper<argument>> m_arguments;
    };
    Thus, one still has to deal with raw pointers, smart pointers or reference wrappers within a range-based for loop:
    Code:
    container c;
    for (auto& object:c) {
        object.get().some_function(); // would be great to use `object.some_function()' instead
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, "." forwarding isn't possible in the language (yet; there's actually a suggestion for that for C++17, though). The closest you can come is ->. Otherwise you'll have to end up writing a proxy class for your argument, so that it can take all the member functions you want to call directly on the object and forward those.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Range-based for loops in C++11
    By webmaster in forum C++ Programming
    Replies: 4
    Last Post: 09-12-2011, 04:55 PM
  2. range based for command
    By tabl3six in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2011, 07:14 AM
  3. Reversing the traversal of c++0x's range based for
    By Mozza314 in forum C++ Programming
    Replies: 29
    Last Post: 03-15-2011, 06:11 PM
  4. Looping Text based battle
    By pujuman in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2004, 05:24 AM