home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12445 < prev    next >
Encoding:
Text File  |  1992-08-17  |  2.1 KB  |  67 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!caen!sdd.hp.com!apollo.hp.com!netnews
  3. From: vinoski@apollo.hp.com (Stephen Vinoski)
  4. Subject: template static data members
  5. Originator: vinoski@zep_r.ch.apollo.hp.com
  6. Sender: usenet@apollo.hp.com (Usenet News)
  7. Message-ID: <Bt5FDH.DFK@apollo.hp.com>
  8. Date: Mon, 17 Aug 1992 22:20:05 GMT
  9. Nntp-Posting-Host: zep_e.ch.apollo.hp.com
  10. Organization: Hewlett-Packard Company, Apollo Division - Chelmsford, MA
  11. Lines: 54
  12.  
  13.  
  14. No else seems to have reported this problem, so I thought I'd share it
  15. with the net.
  16.  
  17. Given these declarations:
  18.  
  19.     class Simple
  20.     {
  21.       public:
  22.         Simple() { // ... }
  23.  
  24.         // rest of class...
  25.     };
  26.  
  27.  
  28.     template<class T>
  29.     class LessSimple
  30.     {
  31.       public:
  32.         // rest of class...
  33.  
  34.       private:
  35.         static Simple s;
  36.     };
  37.  
  38. and this definition for the template static data member:
  39.  
  40.     template<class T> Simple LessSimple<T>::s;
  41.  
  42. Some compilers derived from cfront 3.0 won't be able to handle the
  43. instantiation of the ``s'' data member template correctly, and will
  44. complain about unresolved template references at link time.
  45.  
  46. The problem is due to the fact that uninitialized static data members
  47. are put into the bss section, and the template instantiation system
  48. does not search the bss section in order to resolve template
  49. references.  The reason the bss section is not searched is because the
  50. language rules state that static data members must have one definition
  51. per program.  If the definition does not appear, the compiler adds the
  52. symbol to the bss section, and therefore missing definitions can be
  53. detected simply by not searching bss.  Good try, but...
  54.  
  55. Of course, this problem can be avoided by explicitly initializing
  56. static data members in their definitions, so that they wind up in the
  57. data section rather than in bss.  Unfortunately, in the example case
  58. above, ``s'' cannot be explicitly initialized, and the problem cannot
  59. be solved without changing the declaration of Simple so such
  60. initialization is allowed (if possible).
  61.  
  62. -steve
  63. -- 
  64. Steve Vinoski  (508)436-5904   vinoski@apollo.hp.com
  65. Distributed Object Computing Program
  66. Hewlett-Packard, Chelmsford, MA 01824       These are my opinions.
  67.