Thread: How properly inherit from template?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I realize C# or .NET and C++ are different things, but I do like how the .NET framework is laid out. That is why I like things to be part of classes and not broken out. It also makes me take distance from standard library functions that clearly does things in a bad in my eyes.

    Yes, they're all respected, they've got loads of experience and what they say may be right. I do respect them, but I don't always agree with them.
    I like .NET layout kind of way, but I don't .NET, so I'm mostly re-engineering C++ in my own way of end without breaking C++ concepts.
    Perhaps it can be done, perhaps not, but I'm certainly not going to just give up.

    Oh and about the virtual destructors thing... I'm just thinking "should" is too strong a word. It's not a requirement, it's good practice, therefore it is strongly recommended in my view. Not should and not must.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You have a weird definition of "should".
    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

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    You have a weird definition of "should".
    It's probably because the would should translates (using "trivial" translation) to "skall" in Swedish, but I think the Swedish word is stronger than the English word.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm just thinking "should" is too strong a word.
    Here I was thinking "must" is spot on. If a class may be crafted polymorphically a virtual destruction mechanism must be provided or it is wrong. If, by requiring the use of some sort of factory, you do not disallow construction/allocation, then the mechanism must be provided by a 'virtual' destructor. If you've not fulfilled this your code is broken for any such behavior. (Yes, the STL containers are broken in this aspect; primarily because it lacks separation of interface and implementation.)

    Soma

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the STL containers are broken in this aspect; primarily because it lacks separation of interface and implementation.
    I had the impression that the standard containers are not broken in this respect; they were simply not designed to be polymorphic base classes.
    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. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I had the impression that the standard containers are not broken in this respect; they were simply not designed to be polymorphic base classes.
    Hmmm...

    In the case of the STL containers, I would say broken, but one might instead stay deficient-by-design. Personally, I can't imagine that none on the committee, the lot of them being very intelligent, ever thought "Hey, I just bet people will try using them polymorphically from a runtime construct because they provide a very nice and consistent interface". (The compile time polymorphisms provided by the STL are... beyond genius.) So, the only possibility to me, they simply failed to get the specification as good as it might have been in the time they had; thus we have a few broken bits here and there. (I blame the lack of time and apparent pressure against templates for virtually all of the bad bits of the specification.) This is, by the by, what I mean by
    "because it lacks separation of interface and implementation".

    Soma
    Last edited by phantomotap; 04-23-2008 at 08:22 AM.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the case of the STL containers, I would say broken, but one might instead stay deficient-by-design. Personally, I can't imagine that none on the committee, the lot of them being very intelligent, ever thought "Hey, I just bet people will try using them polymorphically from a runtime construct because they provide a very nice and consistent interface".
    Well...

    "STL is not object oriented. I think that object orientedness is almost as much of a hoax as Artificial Intelligence. I have yet to see an interesting piece of code that comes from these OO people."
    - Stepanov, An Interview with A. Stepanov
    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

  8. #23
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yes, I've read it. It isn't particularly interesting. From the same interview:

    I find OOP methodologically wrong. It starts with classes.
    Only when you understand them well, can you come up with an interface that will let them work.
    My approach works, theirs does not work. Try to implement a simple thing in the object oriented way, say, max. I do not know how it can be done.
    Code:
    template <class StrictWeakOrdered>
    inline StrictWeakOrdered& max(StrictWeakOrdered& x,
    StrictWeakOrdered& y) {
    return x < y ? y : x;
    }
    template <class StrictWeakOrdered>
    inline const StrictWeakOrdered& max(const StrictWeakOrdered& x,
    const StrictWeakOrdered& y) {
    return x < y ? y : x;
    }
    Inheritance and interfaces don't help. And if they cannot implement max or swap or linear search, what chances do they have to implement really complex stuff?
    All of this adds up to someone trying to sell you something. His "implementation" does nothing but forward to another function which in virtually all cases must be specifically implemented for each class--indeed, many classes require several such functions providing a "strict weak ordering" for several different metrics. In reality, "OOP" is just an umbrella term for a handful of techniques; many of which he makes use of... whether he likes it or not.

    The only thing the article might show, if that is really your view, is that the STL is indeed deficient-by-design. If you say that something deficient-by-design isn't broken... I will not argue with you. (I do, obviously, disagree.)

    Soma

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    Well...

    "STL is not object oriented. I think that object orientedness is almost as much of a hoax as Artificial Intelligence. I have yet to see an interesting piece of code that comes from these OO people."
    - Stepanov, An Interview with A. Stepanov
    Wow, he's a pretty opinionated guy. :-) I'd like to see a debate between him and say, a Robert Martin or Grady Booch.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I've read it. It isn't particularly interesting.
    I think that it is interesting to read what that fellow was thinking of when he designed the STL that was incorporated into the standard library. It also helps to explain why the committee did not turn the STL containers into polymorphic base classes: they had little time by then to properly retrofit the STL in such a way, even if they felt that it was worth it.

    In reality, "OOP" is just an umbrella term for a handful of techniques; many of which he makes use of... whether he likes it or not.
    I agree, and that is why Stepanov ended up designing the STL in the context of an object oriented language. Also, the basis for the STL's generic programming idiom is the iterator pattern, which was formally documented in OO literature around the time he actually designed the STL.

    Wow, he's a pretty opinionated guy. :-) I'd like to see a debate between him and say, a Robert Martin or Grady Booch.
    Or another opinionated fellow, Linus is his first name, who has spoken out against C++ and the STL
    Last edited by laserlight; 04-23-2008 at 09:23 AM.
    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

  11. #26
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It also helps to explain why the committee did not turn the STL containers into polymorphic base classes: they had little time by then to properly retrofit the STL in such a way, even if they felt that it was worth it.
    O_o

    Me from earlier in the evening:

    So, the only possibility to me, they simply failed to get the specification as good as it might have been in the time they had; thus we have a few broken bits here and there.
    Soma

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Me from earlier in the evening:
    You also wrote:
    I blame the lack of time and apparent pressure against templates for virtually all of the bad bits of the specification.
    I am just pointing out that there was pressure against OO too, though from outside the committee, but presumably supported (perhaps ironically) by Stroustrup.

    Still, I am not sure if this is really a case of "they simply failed to get the specification as good as it might have been". After all, even in Java, inheriting from the standard containers is discouraged in favour of composition.
    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

  13. #28
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    After all, even in Java, inheriting from the standard containers is discouraged in favour of composition.
    Not really... depending on what you mean by "inheriting". Extending and implementing the interfaces is expected, even encouraged. Inheriting from an implementation, as for example "java.util.hashset" is probably discouraged. I'm fine with that. (This fits in rather nicely with "separation of interface and implementation".)

    Soma

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not really... depending on what you mean by "inheriting". Extending and implementing the interfaces is expected, even encouraged. Inheriting from an implementation, as for example "java.util.hashset" is probably discouraged.
    Yes, that is what I mean: inheriting from a concrete class, since the C++ standard containers are concrete classes.

    This fits in rather nicely with "separation of interface and implementation".
    I agree.
    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

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

    We certainly seem to have hijacked this thread "most excellently" considering we seem to agree on everything. ^_^

    Soma

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