Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring namespace as macro - C++

In standard library, I found that namespace std is declared as a macro.

#define _STD_BEGIN  namespace std {
#define _STD_END        }
  1. Is this a best practice when using namespaces?
  2. The macro is declared in Microsoft Visual Studio 9.0\VC\include\yvals.h. But I couldn't find the STL files including this. If it is not included, how it can be used?

Any thoughts..?

like image 941
Navaneeth K N Avatar asked Feb 25 '10 04:02

Navaneeth K N


2 Answers

Probably not a best practice as it can be difficult to read compared to a vanilla namespace declaration. That said, remember rules don't always apply universally, and I'm sure there is some scenario where a macro might clean things up considerably.

"But I couldn't find the STL files including this. If it is not included, how it can be used?".

All files that use this macro include yvals.h somehow. For example <vector> includes <memory>, which includes <iterator>, which includes <xutility>, which includes <climits>, which includes <yvals.h>. The chain may be deep, but it does include it it some point.

And I want to clarify, this only applies to this particular implementation of the standard library; this is in no way standardized.

like image 62
GManNickG Avatar answered Oct 22 '22 21:10

GManNickG


  1. In general No. The macros were probably used at the time when namespaces were not implemented by some compilers, or for compatibity with specific platforms.
  2. No idea. The file would probably be included by some other file that was included into the STL file.
like image 3
amit kumar Avatar answered Oct 22 '22 20:10

amit kumar