home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,366 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Definition of static const member va   
   13 Jun 11 16:44:10   
   
   6fb47588   
   From: daniel.kruegler@googlemail.com   
      
   Am 13.06.2011 10:58, schrieb A. McKenney:   
   > I'm getting a warning message from a compiler for   
   > a construct that I thought was required by the standard.   
   >   
   > In  A.h (included in mulitple files):   
   >   
   >      class XXX { public:  static const int DefaultX = 1; };  //   
   > declaration of XXX::DefaultX   
   >   
   > In A.cpp:   
   >   
   >      const int XXX::DefaultX;  // definition of XXX::DefaultX   
   >   
   > With VC++ 2010, this results in a multiply-defined   
   > warning message for XXX::DefaultX.   
      
   This compiler diagnostics is very unfortunate and should not occur in a   
   good quality compiler unless the definition of XXX::DefaultX in A.cpp is   
   not the only one. Are you sure that you have not provided more than one   
   such definition? Have you possibly *included* A.cpp in another   
   translation unit?   
      
   It is definitively correct that   
      
   const int XXX::DefaultX;   
      
   is the correct way to *define* XXX::DefaultX, therefore this definition   
   is only optional unless your program does not ODR-use XXX::DefaultX.   
      
   According to [class.static.data] p2:   
      
   "The declaration of a static data member in its class definition is not   
   a definition [..] The definition for a static data member shall appear   
   in a namespace scope enclosing the member's class definition."   
      
   and p3 which describes the special case of const and non-volatile   
   literal type used as static data meber that has an initializer at the   
   in-class declaration (that is your case):   
      
   "[..] The member shall still be defined in a namespace scope if it is   
   odr-used (3.2) in the program and the namespace scope definition shall   
   not contain an initializer."   
      
   > If I comment out the definition (in A.cpp),   
   > everything compiles fine&  without errors.   
   >   
   > (The Solaris 10 compiler is happy either way.)   
   >   
   > My understanding of the standard was   
   > that the definition is required.   
      
   Well, the definition is required, if the program ODR-uses the variable,   
   but I would consider above warning as very misleading, because you   
   usually don't know, whether somewhere in the future the variable might   
   be odr-used, if it currently is not.   
      
   > Am I wrong?  Or is VC++ wrong?   
      
   If your program really provides a single definition of the static data   
   member as you describe above I consider this a compiler defect.   
      
   HTH & Greetings from Bremen,   
      
   - Daniel Krügler   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca