Monday, April 1, 2019

c++ - Is there a way to ensure that a code only uses names from std, and not the global namespace?

When using a header which has a format , an implementation will put names into std namespace. And it may put names into the global namespace as well, as it is described here:




[ Example: The header assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. [...] — end example ]





Is there a (maybe compiler dependent) way to circumvent/disable this behavior (I'm open to any tricky solution)? I'd like to use names from std only, and I'd like to have an error/warning when using names from the global namespace:



#include 

double a = cos(0.5); // I'd like to have an error here, because std:: is missing


The reasons:





  • It is hard to write a portable code, if it may use names from the global namespace, as these names may be not available in other compilers. It is much cleaner to use everything from std, and not use the global namespace at all

  • cos(0.5f) does a different thing whether std:: is prefixed or not (float vs double result).

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...