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 6223493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:30:00+00:00 2026-05-24T08:30:00+00:00

I recently saw this code being used in a source file in a C++

  • 0

I recently saw this code being used in a source file in a C++ project:

using namespace std;
#include <iostream>

Ignoring all issues of whether it’s a good idea to have using namespace std at all, is the above code even legal? There is no code in the file before these two lines.

I would have thought that this wouldn’t compile, since namespace std hasn’t been declared in scope until the #include <iostream> directive includes it into the file, but using the build system for the project this was compiling just fine. If someone has a link to a relevant part of the spec, that would be most appreciated.

  • 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-24T08:30:00+00:00Added an answer on May 24, 2026 at 8:30 am

    A perhaps interesting data point. When I compile the following:

    using namespace std;
    using namespace no_such_namespace;
    

    with g++ 4.5.2, I get:

    c.cpp:2:17: error: ‘no_such_namespace’ is not a namespace-name
    c.cpp:2:34: error: expected namespace-name before ‘;’ token
    

    To be clear, those two lines are the entire source file I compiled.

    Neither std nor no_such_namespace has been defined as a namespace at that point, but g++ complains only about the second. I don’t think there’s anything special about the identifier std in the absence of a declaration of it. I think @James Kanze is right that this is a bug in g++.

    EDIT: And it’s been reported. (5 years ago!)

    UPDATE: Now it’s more than 8 years, and still hasn’t been assigned to anyone, much less fixed. g++ 4.9.2 exhibits the problem. clang++ 3.5 doesn’t, but it issues a warning for std and a fatal error for no_such_namespace:

    c.cpp:1:17: warning: using directive refers to implicitly-defined namespace 'std'
    using namespace std;
                    ^
    c.cpp:2:17: error: expected namespace name
    using namespace no_such_namespace;
                    ^
    1 warning and 1 error generated.
    

    UPDATE: As of 2021-09-24, the bug report is still open and the bug exists in g++ 11.2.0. A comment posted 2021-07-24 suggests that g++ should warn about this.

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

Sidebar

Related Questions

I recently saw a bit of code that looked like this (with sock being
I recently saw this code on another post ( jQuery Set Cursor Position in
I recently saw code that looks like this: (function (someGlobal) { someGlobal.DoSomething(); })(someGlobal); where
I recently saw some code that reminded me to ask this question. Lately, I've
Was recently reviewing some Java Swing code and saw this: byte[] fooReference; String getFoo()
Recently I saw this piece of JavaScript code, but have been unable to understand
Recently I saw some code like this: <tr> <th> Some label: </th> <td> <input
In some code I saw recently there was a structure defined like this: typedef
I had to delve into some VB6 code recently and I saw this pattern
Recently i saw this piece of code. Shouldnt this line be a compile error?

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.