简体   繁体   中英

Namespace Aliasing in C++

It is widely known that adding declarations/definitions to namespace std results in undefined behavior. The only exception to this rule is for template specializations.

What about the following "hack"?

#include <iostream>

namespace std_
{
  void Foo()
  {
    std::clog << "Hello World!" << std::endl;
  }

  using namespace std;
}

int main()
{
  namespace std = std_;

  std::Foo();
}

Is this really well-defined as far as the standard is concerned? In this case, I'm really not adding anything to namespace std , of course. Every compiler I've tested this on seems to happily swallow this.


Before someone makes a comment resembling "why would you ever do that?" -- this is just to satisfy my curiosity...

Redefining std as an alias is okay, as long as you are not in the global declarative region:

In a declarative region, a namespace-alias-definition can be used to redefine a namespace-alias declared in that declarative region to refer only to the namespace to which it already refers.

Since you define the alias in main() , it shadows the global std name. That's why this works, should work, and is perfectly fine according to the standard. You're not adding anything to the std namespace, and this "hack" only serves to confuse the human reader of the code.

Inside main , after the namespace alias definition, std refers to the alias std for the std_ namespace. The "usual" std namespace is hidden, much as a function local variable would hide a global variable with the same name.

You're not adding anything to std:: . You're adding to std_:: , then declaring an alias named std:: .

I never knew that there is a rule you can't add to std:: . Is that correct? If so, I'm guessing that the rule only exists to reserve the namespace for future expansion. So you really don't have to hack anything if you really want to augment std:: .

Someone post if you know of a compiler that does NOT allow such a thing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM