home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1907 < prev    next >
Encoding:
Text File  |  1992-12-24  |  4.6 KB  |  92 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Must derived class reserve space for an empty base class?
  5. Message-ID: <1992Dec24.003604.701@microsoft.com>
  6. Date: 24 Dec 92 00:36:04 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec16.202800.3398@microsoft.com> <5450@holden.lulea.trab.se>
  9. Lines: 81
  10.  
  11. In article <5450@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  12. |Jim Adcock (jimad@microsoft.com) wrote:
  13. |: In article <5386@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  14. |: |That two non-generic pointers of the same type (possibly converted from
  15. |: |pointer-to-derived) to different objects or data members should be
  16. |: |guaranteed to compare unequal.
  17. |
  18. |Your example (below) shows that at least we have established what we
  19. |are disagreeing on.
  20.  
  21. No, it merely establishes one thing that we are disagreeing on.
  22.  
  23. |But the cases where "unnecessary" padding would be required would
  24. |have very minor impact on the majority of programs, at least if I
  25. |may consider the current implementation of virtuals (vtbl).  After all,
  26. |inheriting from a truly empty base class (no virtuals) is quite uncommon,
  27. |don't you think?
  28.  
  29. No.  Since the use of vtables and vtptrs is an implementation decision,
  30. not a language decision, classes are "empty" if they have no member 
  31. objects, regardless of whether or not these classes include pure 
  32. virtual functions.  There is no requirement that implementatins put
  33. vtptrs where you are assuming they go.  There is no requirement that
  34. implementations even *have* vtptrs.
  35.  
  36. Given that the vtptr issue is an implementation decision, I'd say on
  37. the contrary "empty" base classes are quite common.  As in pure virtual
  38. base classes.
  39.  
  40. |If pointer distinctness (as defined above) allows useful semantic constructs,
  41. |such as the very common idiom for operator=:
  42. |"if ( &other == this ) return", then a small and rare storage overhead
  43. |in examples such as the one below could easily be tolerated (IMHO).
  44.  
  45. Other people on the net have complained loudly, repeatedly, and
  46. correctly [IMHO] about implementations that consume space when
  47. inheriting from a pure virtual base classes.
  48.  
  49. |: If you want object addresses to be
  50. |: unique, then write your code so that they are unique.
  51. |
  52. |You cannot always guarantee that.  Suppose you create a class template
  53. |that uses pointers to an unknown class (T*) for identity tests (the
  54. |template could be specified to handle only statically allocated objects,
  55. |if you like).  That template cannot know that the type parameter (T)
  56. |specifies a class that will have distinct pointers to all instances,
  57. |_unless_ pointer distinctness is universally guaranteed.
  58.  
  59. Again you are trying to insist that your template class works for
  60. every and all target types which is pure and simple nonsense.
  61. Templates can and will always only work on target types that implement
  62. not only the right syntax, but also the right semantics.  THE ONLY WAY
  63. a template can know that the target type supports the right semantics
  64. is for the template writer to SPECIFY the semantics, and for the template
  65. invoker to CORRECTLY match such.  Otherwise all bets are off, even
  66. if the syntax happens to match and the template compiles without 
  67. compile-time error.  Again, this is nothing new, and has nothing to
  68. do with pointers.  If the template writer expects certain correspondences
  69. between pointers and object identities, the template writer should specify
  70. such, and the template invoker must correctly implement such for the
  71. target type.  Again, this is nothing new.
  72.  
  73. |There are existing commercial class templates that rely on pointer
  74. |distinctness.  Are they invalid?
  75.  
  76. Of course they're "valid" -- they're simply not strictly portable.
  77. They meet the needs of a portion of the marketplace, and do not
  78. meet the needs of others.  If they want to meet the needs of a greater
  79. portion of the marketplace, then they need to write their classes
  80. to rely on even fewer implementation dependences.
  81.  
  82. In practice "portability" is never an absolute, its only a relative
  83. term.  In practice, no interesting program or library is "strictly
  84. portable".  Interesting programs or libraries only have sections of
  85. code that can be informally referred to as "portable" and other
  86. sections of code that are clearly not strictly portable.  By
  87. making things more portable the implementor makes porting the code
  88. less expensive, while typically raising BOTH the cost of implementing
  89. that code AND the cost of executing that code.   While we tend, as
  90. a rule of thumb, think of more portable code as being better, in
  91. reality such comparisons are only relatively true.
  92.