Though implementation problem

This is a discussion on Though implementation problem within the C++ Programming forums, part of the General Programming Boards category; The idea is that an index is not an integer. 1 or 5 may not be valid indexes. Only an ...

  1. #196
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    The idea is that an index is not an integer. 1 or 5 may not be valid indexes. Only an iterator is a valid index. That was the idea anyway.
    If an index supplied is invalid, what's wrong with throwing std::out_of_range?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #197
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Oh, I will be doing that alright.
    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.

  3. #198
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    You are putting the burden of constant correctness on the wrong code! You can't protect the contents of an object by making it merely difficult, and you can't make it impossible.

    Also, returning an integer versus returning an object is irrelevant to your 'const' concerns if you are going to provide a 'difference' function and an ability to convert an 'iterator' to an 'const_iterator'.

    Soma

  4. #199
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by phantomotap View Post
    You are putting the burden of constant correctness on the wrong code! You can't protect the contents of an object by making it merely difficult, and you can't make it impossible.
    I realized.

    Also, returning an integer versus returning an object is irrelevant to your 'const' concerns if you are going to provide a 'difference' function and an ability to convert an 'iterator' to an 'const_iterator'.
    But I never claimed to make any difference function to convert between const_iterator/iterator.
    I was admitting I was planning one to convert to a STL iterator.
    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. #200
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    But I never claimed to make any difference function to convert between const_iterator/iterator. I was admitting I was planning one to convert to a STL iterator.
    A 'difference' function to obtain the mathematic distance between two instances could be written--unless you also have changed the meaning of '+' and '==', and if I can't obtain an 'const_iterator' from an 'iterator' your interface is useless and horribly broken because I couldn't call 'const_iterator' code from any 'iterator' code.

    Soma

  6. #201
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Your fears are, fortunately, unfounded.
    A const_iterator can be built from an iterator.
    Initially, I HAD a Difference function that returned the distance between the two iterators. It was a friend function and subtracted the pointers directly.
    But I've changed the behaviour now, more to STL like.

    Alright, the first test is a success! I have successfully implemented a custom iterator and re-written a stand-alone Replace function to use iterators and std::copy_backward and std::copy.
    More tests to be done, of course.
    Last edited by Elysia; 05-15-2008 at 10:54 AM.
    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.

  7. #202
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    Your fears are, fortunately, unfounded.
    So I imagined.

    But I've changed the behaviour now, more to STL like.
    Now if we could just break you of the habit of sticking a 'C' in front of things...

    Alright, the first test is a success! I have successfully implemented a custom iterator and re-written a stand-alone Replace function to use iterators and std::copy_backward and std::copy.
    More tests to be done, of course.
    Congratulations! Admit it: now that your coding with it, the STL, you love it. ^_^

    Soma

  8. #203
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by phantomotap View Post
    Now if we could just break you of the habit of sticking a 'C' in front of things...
    Now way
    Classes are classes and therefore must begin with C

    Congratulations! Admit it: now that your coding with it, the STL, you love it. ^_^
    Sure, it's good to be able to use STL, but I'm still going to provide interfaces around them in my classes!

    Now I'm going to have to overload the std::backward_copy and std::copy, and that probably means the second stage of the plan, because the current implementation only works if the buffer does not need to be resized...
    Last edited by Elysia; 05-15-2008 at 11:04 AM.
    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. #204
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    Now I'm going to have to overload the std::backward_copy and std::copy, and that probably means the second stage of the plan, because the current implementation only works if the buffer does not need to be resized...
    That is how they should work! I don't know about you, but if I swapped the bits around I would want it to warn/crash/raise an exception by default, not just push additional data onto the string. I might make a programming mistake. Just grab a 'back_insert_iterator<your_string<?>>' with 'back_inserter(instance)'. (Assuming you have the relevant 'push_back()' method.)

    Soma

  10. #205
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    But... it should work with std::copy even if there's no space! It calls the overloaded class operators! Darn!
    More debugging needed -_-
    Or maybe it really doesn't work... I'll just have to find out.
    EDIT: Duh, of course it doesn't!
    It does the work via iterators, not the string class's operators!
    Last edited by Elysia; 05-15-2008 at 11:12 AM.
    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.

  11. #206
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    But... it should work with std::copy even if there's no space!
    Er... what? O_o

    Soma

  12. #207
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Still unused to iterators and std::XXX family...
    Of course it's an error.
    I need to allocate proper space first or forward operator * calls to the string class!
    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.

  13. #208
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Quote Originally Posted by Elysia View Post
    But... it should work with std::copy even if there's no space! It calls the overloaded class operators! Darn!
    More debugging needed -_-
    At least read how std::copy is supposed to work, and realize that you are doing something that nobody expects. In the standard algorithm nothing is supposed to be resized; it's stated in one of the preconditions.

    There is enough space to hold all of the elements being copied. More formally, the requirement is that [result, result + (last - first)) is a valid range. [1]
    So correct copying would be:
    Code:
    vector<o> foo;
    // ... use foo ...
    list<o> bar( foo.size( ) );
    copy( foo.begin( ), foo.end( ), bar.begin( ) );
    I assume they do it this way so as to allow the containers to manage their own memory, or if we apply Occam's razor, std::copy does little more than perform assignments in a loop. It shouldn't be allocating buffers for anything because that makes copying more work than it needs to be. Do you want to throw exceptions when you copy?

    So do it this way, yes?

  14. #209
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Yes. I have to make sure to resize the string to proper size. Only, it invalidates all iterators, which can be pretty annoying. I'll have to manually fix that until I can fix the new implementation.

    BTW, the TYPE_FRIENDLY macro only seems to work for VS (at least I think it does - I get no compile error). But for GCC, it does not.
    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. #210
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by citizen View Post
    So correct copying would be:
    Code:
    vector<o> foo;
    // ... use foo ...
    list<o> bar( foo.size( ) );
    copy( foo.begin( ), foo.end( ), bar.begin( ) );
    I assume they do it this way so as to allow the containers to manage their own memory, or if we apply Occam's razor, std::copy does little more than perform assignments in a loop. It shouldn't be allocating buffers for anything because that makes copying more work than it needs to be.
    But there is a special iterator, that is produced by back_inserter. So copy could also ask the receiver to make more room:
    Code:
    vector<o> foo;
    //.. use foo ...
    //list<o> bar( foo.size( ) ); //not necessary
    copy( foo.begin( ), foo.end( ), back_inserter(bar) );
    So, perhaps Elysia needs something akin to the back_inserter?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 12:59 AM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21