Menu

any namespace in a header file

Kurgusov
2007-09-08
2012-09-26
  • Kurgusov

    Kurgusov - 2007-09-08

    Hope your all well,

    A while back Clifford advised me declaring a namespace (std in the conversation)
    in a header file was bad.

    I cant find any links on the net,they just talk 'bout namespace syntax etc.

    My friend who programs machinary movement in an industrial company even declares his namespaces in header files?

    Please can I get the reason its not a good thing to do!

    respects.

     
    • Soma

      Soma - 2007-09-08

      Clifford is correct.

      "Old Newbie" may of may not be correct. (The differing language confused the issue.)

      Virtually everyone will tell you not to put 'using' directives in headers. The ones who say that it is an okay practice are wrong. Regardless of circumstances it will be evil hackery, and, much more likely, it will be garbage and crap design.

      Instead of whatever you are searching for search for "C++ Using Directive Headers". It is a universal evil.

      In defense of whatever "Old Newbie" may have been saying: Boost does put 'using' directives in headers. They do not put any global 'using' directives in the headers. Any uses are nested carefully in such a way that it does not interfere with the "outside world". This is the kicker, it is not used, as most people believe, to simplify Boost coding. The cases in Boost are necessary, and still evil, hackery to support compilers with and without certain support for namespaces correctly.

      That said, you are confused by the issue itself.

      What you mustn't do: put 'using' directives within a header.
      What you must do: declare namespaces within a header.

      That is, "using namespace std;" is a using directive, and "namespace myns{/stuff/}" is a namespace declaration and must be visible to the compiler for lookup to work.

      Soma

       
    • Anonymous

      Anonymous - 2007-09-08

      I feel that Soma's clarification is useful, but besides that, I must point out that my comment is in the spirit of the OP assertion (the quotes are mine):

      >> A while back Clifford advised me declaring a namespace [...] in a header file was bad.

      and in my prevous post:

      >> [...] use namespaces in headers [..]

      As You can see there aren't any mention to 'using' directives in headers. Even more, many authors state that the "using directives" should be avoided as possible even in the code, because they pollute the code from the point of it's declaration.

      Old Newbie.

       
      • Soma

        Soma - 2007-09-08

        shrug

        I was giving you the benefit of the doubt, but as I said, I wasn't sure what you may have been saying because, unlike the OP I know the thread in question, it could be read either way in this context. And, yea, I realize that this is because the OP confused the meaning of the words.

        "As You [...] in headers."

        Not by you; not even by the OP, but by Clifford in the relevant instructional post.

        Soma

         
    • Anonymous

      Anonymous - 2007-09-09

      Since it referred to a comment I made, let me explain what I meant (in fact I think I explained it when I said it!)

      You have to realise that namespaces are used to prevent symbol clashes. If you use say "using namespace std ;" in a header file you you not only move the std:: symbols to the global (::) namespace within that file, but also in the file in which the header is included and any diles included after that one.

      So, say you create a library and release a header that does this, a user of your library will include your header and have your decision about what to to with std:: imposed upon him - and if he never looks in the header, may never know it - leading to some really hard to figure compilation errors. The std:: namespace is full or really trivial symbol names that would be commonly used in code for entirely unrelated purposes.

      Besides problems with other peoples code, it will cause problems with your own. An apparently simple code maintenance edit can fail to build because of a symbol clash.

      Clifford

       
    • Anonymous

      Anonymous - 2007-09-08

      Perhaps there are not universal truths, and what Clifford said that in some context (it can clarify the point if want), but despite what can it say, virtually all serious software and librarys that you can find over there use namespaces in headers. As an example you can have a look to the Boost librarys, who by the way aren't no suspicious to be an example of amateurish work.

      (I hope that despite my weak english you can understand what I mean)

      Old Newbie.

       
    • Kurgusov

      Kurgusov - 2007-09-08

      yeah right, dont put using directives in headers,do declare them in headers,

      thanks for the help you two,much appreciated,
      Ill check out "C++ Using Directive Headers" now.

      thanks.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.