Thread: Providing my own simple iterators

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your begin and end functions should return your iterator, not vector's iterator. Although in this example, returning it directly works just fine because all you're doing is creating a wrapper around it.
    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.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If it is a wrapper, which is odd because that's not what I read from the description of the problem, then you might as well make PointsIterator a typedef of the vector<double>::iterator. I thought that something more complex was happening.

  3. #18
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I felt what you say when I was writting the code. So, if I do only the typedef (notice that I had it in my previous code as a comment), then I am ok (without writing the wrapper), right?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. But again, I don't think it makes sense to "iterate" a point. It would be better to use appropriate names for the member functions that iterate the point (i.e. don't name them begin and end).
    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.

  5. #20
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok, thanks everybody and sorry for my latency to get the idea.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This strikes me as silly.

    Can you explain why a Point class would need to contain a vector, and why it would need to permit the caller to iterate over that vector?

    By giving your Point class operators that expose iterators that act on the vector, you're exposing the private vector anyway.

    Rather than defining an internal class, all your Point class needs to do is expose a few typedefs, such as
    Code:
    typedef std::vector<double>::iterator PointsIterator;
    and a few forwarding functions, such as
    Code:
    PointsIterator begin() {return v.begin();}
    It all still strikes me as being a pointless exercise, and (even if one accepts the premise that it is not pointless) one you've made much more complicated than it needs to be.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yep. that's what we said above! A typedef, end() and begin() seem to be enough.

    The Point has coordinates. Iterator allows the derived class to produce more efficient code, because of some reasons that are out of scope.

    (However, I am not arguing that what I do is the best approach, but I have to start from somewhere )
    Last edited by std10093; 04-27-2014 at 03:20 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, but typically one does not iterate coordinates. It's usually bundled in some sort of struct and returned by some function GetCoordinates.
    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.

  9. #24
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    And what GetCoordinates return? Maybe a pointer to the vector of coordinates?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #25
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I knew it.

    No more piecewise bull........ std10093, post a complete and meaningful example in pseudocode.

    Why pseudocode? Because you aren't C++ well enough to post C++ code.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    And what GetCoordinates return? Maybe a pointer to the vector of coordinates?
    Code:
    struct Coordinates
    {
        float x, y, z;
    };
    
    class XMyClass
    {
        // ...
    
        Coordinates GetCoordinates() const { return m_Coordinates; }
    
        // ...
    
        Coordinates m_Coordinates;
    };
    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.

  12. #27
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    As an aside, can we std::move() it, move it?
    Code:
    struct Coordinates
    {
        float x, y, z;
    };
     
    class XMyClass
    {
        // ...
     
        // Is this Kosher?
    
    
        Coordinates GetCoordinates() { return std::move(m_Coordinates); }
     
        // Or does return value optimization already kick in here?
    
        // ...
     
        Coordinates m_Coordinates;
    };
    Also, a 12 byte structure seems like it'd be awful for alignment, right? Can we just pad our structure with an extra 4 bits for easier alignment in memory? Is this even a thing? I think I read about it but I feel compelled to ask to improve potential performance of the code.

    Edit : I'm an idiot. You should never use std::move() in this context because you want to actually keep m_Coordinates in a specific state, duh! But my question should rather be, what if I have a function that takes 3 floats as input and returns a point? Is std::move() okay there? Consider,

    Code:
    struct point {
        float x, y, z;
    
       point(float x, float y, float z) {
           this->x = x; this->y = y; this->z = z;
       }
    };
    
    point make_point(float x, float y, float z) {
    
        return std::move(point(x, y, z));
    }
    This is a poor and trivial example, I do suppose. Hmmm... Maybe constructors just really are the answer... I was just wondering if it's ever okay to use return statements with std::move()...
    Last edited by MutantJohn; 04-27-2014 at 12:03 PM.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    o_O
    Moving an internal variable is wrong, wrong, wrong. By moving it, you saying, "I don't need this anymore, so let the new copy just steal my memory/resources/state." We are returning a copy of it here because we don't want the client to modify the coordinates, let alone destroy them! You should only move temporaries because that's what it's for. Member variables aren't temporaries.
    Furthermore, you gain nothing on moving here since it will be the same as a copy. Tell me: how do you implement move semantics on this struct?
    Lastly, let the compiler do its job: if it feels like it should pad the structure, then let it do it. Don't bother yourself. The compiler often knows better.
    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.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Edit : I'm an idiot. You should never use std::move() in this context because you want to actually keep m_Coordinates in a specific state, duh! But my question should rather be, what if I have a function that takes 3 floats as input and returns a point? Is std::move() okay there? Consider,

    Code:
    struct point {
        float x, y, z;
    
       point(float x, float y, float z) {
           this->x = x; this->y = y; this->z = z;
       }
    };
    
    point make_point(float x, float y, float z) {
    
        return std::move(point(x, y, z));
    }
    This is a poor and trivial example, I do suppose. Hmmm... Maybe constructors just really are the answer... I was just wondering if it's ever okay to use return statements with std::move()...
    Returning a local variable is an implicit move in C++11, so you don't need to do it.

    Because you are wondering...
    Code:
    A DangerousFunction(A& a)
    {
        return std::move(a); // Legal, but beware that a is left in a "zombie state", so the caller must not use a after this function
    }
    Last edited by Elysia; 04-27-2014 at 12:16 PM.
    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.

  15. #30
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I googled around and it seems like other people thought they were being clever as well. You're right, using std::move() with a return statement is entirely unnecessary. It's very interesting. I'm glad I asked though because I'm always curious about using things like these and where they're well-suited. Gratned, std::move()'s semantics are entirely over my head. I miss the days when l- and r-value references really were just left and right lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a library or API providing direct access to a running PE address space
    By renzokuken01 in forum Windows Programming
    Replies: 9
    Last Post: 05-24-2009, 01:40 PM
  2. Help with C# iterators
    By dudeomanodude in forum C# Programming
    Replies: 1
    Last Post: 04-28-2008, 03:23 AM
  3. Providing access to member variables between classes
    By Tonto in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2006, 01:06 PM
  4. providing a GUI...novice!
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-20-2002, 09:05 AM