Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:40:59+00:00 2026-05-15T10:40:59+00:00

It is widely known that adding declarations/definitions to namespace std results in undefined behavior.

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:41:00+00:00Added an answer on May 15, 2026 at 10:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is widely known common sense, that for most algorithms, allocating and deallocating data
It is a widely known fact that one shall not stop running processes using
As is widely known, a program running under 32-bit Windows OS has only 2GB
There is a widely known way of locking multiple locks, which relies on choosing
It is widely recommended that JS files should be put at the bottom of
So this may seem like a widely-answered question, but I'm interested more in the
The Unity dependency injection container has what seems to be a widely known issue
As is widely known, anything related to Swing components must be done on the
So I only recently discovered the TODO comment. For those of you that don't
Consider this query that uses SELECT * and 'appends' a calculated column: SELECT *,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.