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 32,777 of 33,346   
   Yordan Naydenov to DeMarcus   
   Re: How do you define a namespace?   
   03 Jan 13 10:41:32   
   
   From: dax_uktc@abv.bg   
      
   "DeMarcus" wrote:   
      
   > Hi,   
   >   
   > I have no question about the namespace in the header file, so for   
   > this post we declare it like this.   
      
   This post is aimed at the technicalities of the namespace but much of   
   it (implicitly) concerns things like readability, clarity, coding   
   rules and standards within an organization and amongst team members.   
   Additionally, if we (entirely) separate the interface from   
   implementation another file might well emerge - the inline file. And   
   we have the same problem in two different places to deal with.   
      
   > // MyClass.hpp   
   > #pragma once   
      
   Yes, it is widely supported, but... non-portable code traded off for   
   optimized compilation... Is it worth it? If the code is meant to be   
   non-portable anyway, then using redundant include guards as   
   (Standard-conforming) alternative is pointless. If portability is   
   regarded a virtue why not combine #pragma once and include guards to   
   take advantage of the former (when it is applicable) and to bypass the   
   disadvantages of the redundant include guards. In any case, include   
   guards must be considered as a must.   
      
   > Now, for the source file I've seen at least three variants.   
      
   Now, for the source and the inline files we have four possibilities:   
      
   > A)   
   > [...]   
      
   Extension definitions of the namespace in the source and in the inline   
   files.   
      
   > B)   
   > [...]   
      
   Explicitly qualifying names of entities lying in the scope of the   
   namespace.   
      
   > C)   
   > [...]   
      
   Using "using directive".   
      
      
   > Are there more variants?   
      
   Yes, another variant exists: using "using declarations".   
   D)   
   // MyClass.cpp   
   #include "MyClass.hpp"   
      
   using MyProduct::MyClass;   
      
   void MyClass::myMethod()   
   {   
        // ...   
   }   
      
   > What are the pros and cons with these?   
   D:   
   [+] more clear and readable code; less typing; entities in the   
   namespace scope not using declared are not presented in the   
   global/local scope thus reducing the risk of name conflicts unlike   
   variant C), using directive;   
   [-] some name conflicts might still emerge; could not take advantage   
   of the name lookup in A), namespace definitions.   
      
      
   And so, my advice (and personal taste) is, in:   
   *.hpp  use (original) namespace definition (what else);   
   *.inl  use (extension) namespace definition (after all *.inl is   
   effectively part of the header which contains some inline code   
   implementation);   
   *.cpp  use "using declarations" to enhance clarity and readability.   
      
   As for the choice of variants presented:   
   - namespace definitions - use them in inline files the same way as in   
   the headers;   
   - explicit qualification - locally in function's body or in *.cpp when   
   it comes to library sources. If too long, some aliasing might be   
   considered;   
   - using directives - try to avoid them altogether;   
   - using declarations - make use of them outside/inside functions in   
   the source files.   
      
   One prevailing rule: Never place a using directive in a header/inline   
   file.   
      
   What's the moral: Hard to say. If you implement a library even the   
   advice about *.cpp is questionable. Stick to a coding of good form and   
   be persistent throughout.   
   And it is helpful not to forget that it must not be argued about   
   taste!   
      
   > PS. It's striking how little information there is about this topic.   
   > I would hope we can come up with a good summary to be put on C++ FAQ   
   > Lite.   
      
   Why not indeed? Cast my 'Yea'.   
      
   Regards,   
           Yordan   
      
      
   --   
         [ 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