Thread: How properly inherit from template?

  1. #106
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Concerning error-proneness, I guess Elysia is saying that
    Code:
    sort(vector.begin(), vector.end();
    has more potential for making typos than
    Code:
    vector.sort();
    However, these functions have a different level of usability. One lets you sort part of the container, and the other sorts all of it. If that is problem for you, nothing can stop you from writing a free function
    Code:
    template <class WritableRangeT>
    void sort(WritableRangeT& r)
    {
        sort(r.begin(), r.end());
    }
    Now, that would be no more error-prone.
    Code:
    sort(vector);
    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).

  2. #107
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Which is exactly what I was hinting at that member functions (or free functions) are less error-prone than algorithms and you should be preferred when possible.
    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. #108
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Elysia View Post
    Which is exactly what I was hinting at that member functions (or free functions) are less error-prone than algorithms and you should be preferred when possible.
    Ok, but how do you write the free functions (or member functions). Would you implement your own sort, because std::sort is so "error-prone"?
    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).

  4. #109
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most likely, I would try to encapsulate std::sort via different member functions in objects, yes. Because it gives the illusion or whatever you want to call it that the object is sorting itself.
    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. #110
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Which is exactly what I was hinting at that member functions (or free functions) are less error-prone than algorithms and you should be preferred when possible.
    Nonsense. See the bit about this in the middle of my post. A programmer isn't more likely to use any alternative correctly. Implementing a large number of decorations, even templates, only changes how they will use it incorrectly. It is always possible to use a method as an interface--to the limits of your compiler; so, where do you draw the line?

    Even the function provided by anon is not an alternative for 'std::sort'. It allows for only a very small sample of the functionality.

    Most likely, I would try to encapsulate std::sort via different member functions in objects, yes.
    So, you would call this method based on the anon decoration 'lexicographic_sort'.

    What do you name this function:

    Code:
    sort(r.begin(), r.end(), _1(&type::accessor1) < _2(&type::accessor1));
    What about this one:

    Code:
    sort(r.begin(), r.end(), _1(&type::accessor2) > _2(&type::accessor3));
    It goes on and on... And according to you inheritance should be preferred so that this too is a method:

    Code:
    sort(r.begin(), r.end(), _1(&type::accessor3) < _2(&type::accessor3));
    Because it gives the illusion or whatever you want to call it that the object is sorting itself.
    ^_^

    Well, that was interesting.

    Soma

    http://cboard.cprogramming.com/showp...&postcount=105

  6. #111
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I fail to see why a vector should be able to sort itself. This kind of thinking makes not even a tiny bit of sense to me. Even Java doesn't have a sort() member function in ArrayList. Instead, there's the static method Collections.sort(). This is Java's way of making it a free function. So even a language like Java, which doesn't even have free functions, is built without the idea that an array should sort itself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #112
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the vector is an object, then naturally you should be able to tell it to sort itself.
    But I'm actually getting tired of these arguments, so I'm only going to respond to sized down posts that I find curious and easy to respond to.
    And phantomotap, I have no idea what that algorithm does or how it sorts data or how it's called, and that's why I prefer to avoid dealing with algorithms.
    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.

  8. #113
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    If the vector is an object, then naturally you should be able to tell it to sort itself.
    But I'm actually getting tired of these arguments, so I'm only going to respond to sized down posts that I find curious and easy to respond to.
    And phantomotap, I have no idea what that algorithm does or how it sorts data or how it's called, and that's why I prefer to avoid dealing with algorithms.
    Go ahead and do whatever you want. We'll stay here in the real world getting real work done.

  9. #114
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And phantomotap, I have no idea what that algorithm does or how it sorts data or how it's called, and that's why I prefer to avoid dealing with algorithms.
    About that:

    Quote Originally Posted by citizen View Post
    Algorithms are abstract since they detail a process, so I don't think that there are valid criticisms beyond what the algorithm does and how it performs. Any attempt to use an algorithm needs an implementation, where your opinions about methods and functions would be applicable. Despite what you may say there are "simpler" implementations of these aside from the choices made in C++, though far be it from me to assume your opinion.
    I made another point in that very post but I have no idea if you actually noticed. Sorry, but I decided to repeat myself. It is getting noisy in here though.

  10. #115
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    If the vector is an object, then naturally you should be able to tell it to sort itself.
    That's not an argument. What's natural about telling a vector to sort itself? Do you feel that sorting themselves is something that comes naturally to vectors? I don't. I think sorting vectors is very much an external operation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #116
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    But I'm actually getting tired of these arguments, so I'm only going to respond to sized down posts that I find curious and easy to respond to.
    Right. Life is not a post count contest. You could try not posting at all, and just accept what people who know more than you are telling you. Being stubborn doesn't make anyone right.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #117
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about responding to everyone's arguments or satisfying their curiousness? Post count means nothing to me.
    I can accept how everything is, but that does not mean I have to do it nor like it. Like it or not, I'm only doing it for fun and not for work.

    Quote Originally Posted by CornedBee
    That's not an argument. What's natural about telling a vector to sort itself? Do you feel that sorting themselves is something that comes naturally to vectors? I don't. I think sorting vectors is very much an external operation.
    If you tell the vector to be an object, which you are, then it should sort itself with what you give it. That's my opinion.
    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. #118
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I were a moderator I sure would close this thread about now. Incidentally, I know I tend to go years upon years without posting, but what happened to Salem's softer side? I mean really...

    Anyway, thats a rhetorical statement, the point is to close the thread...

  14. #119
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... you are right, the debate has spiralled to a dead end.

    Incidentally, I was trying to tell the thread to close itself, but eventually I realised I had to use an external operation to do it.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM