1

I can see the arguments why this would often be considered a bad practice but do any mainstream C++ IDEs provide this functionality, e.g. to automatically use namespace ABC for every .cpp file?

It strikes me adding a using namespace line to a precompiled header (e.g. stdafx.h) would achieve this, but in a less strictly controlled way?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • This is more a question of curiosity than "is it a good idea" – Mr. Boy Sep 10 '15 at 09:53
  • 1
    Precompiled header is the best way to go. Note that some compilers also allow to force include a header. – stgatilov Sep 10 '15 at 09:54
  • 1
    many IDEs have file templates and you may add it here, still don't think you should – RiaD Sep 10 '15 at 09:54
  • @RiaD I'm not really familiar with what you mean - could you explain more maybe in an answer? – Mr. Boy Sep 10 '15 at 09:55
  • If you use precompiled headers, then these would be a decent place to put such using directives. – MicroVirus Sep 10 '15 at 10:02
  • 2
    Putting `using namespace` in headers is a bad idea, using a precompiled header doesn't change that. See http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Jonathan Wakely Sep 10 '15 at 10:06
  • @JonathanWakely doesn't it change things at all? A PCH usually affects all compilation units, whereas a regular header gets included in other headers and could easily 'leak' into other projects. A PCH is rather contained to that project so I'd argue is a bit safer? – Mr. Boy Sep 10 '15 at 10:14
  • 1
    If it comes at the very end of the PCH and you never include other headers after including the PCH then it's safe. Otherwise it still has the potential to alter the meaning of code in other headers that are included after the using-directive. – Jonathan Wakely Sep 10 '15 at 10:31
  • Yeah I see your point. Thanks for clarifying. – Mr. Boy Sep 10 '15 at 10:34

0 Answers0