Skip to content

Remove using namespace std / using-declarations from headers - #364

Merged
zzcgumn merged 5 commits into
developfrom
fix/removing_using_std_from_headers
Sep 2, 2026
Merged

Remove using namespace std / using-declarations from headers#364
zzcgumn merged 5 commits into
developfrom
fix/removing_using_std_from_headers

Conversation

@zzcgumn

@zzcgumn zzcgumn commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

What

using namespace std; (and one namespace-scope using std::chrono::time_point;) appeared at file scope in several headers. That injects names into every translation unit that includes the header, directly or transitively — bad style and a maintenance hazard.

Headers cleaned (declarations now fully qualified)

  • library/src/system/system.hpp
  • library/src/system/thread_mgr.hpp
  • library/src/system/time_stat.hpp
  • library/src/system/timer.hpp
  • library/tests/TestTimer.hpp

The using Clock = std::chrono::steady_clock; alias-declarations are left as-is (they are type aliases, not using-directives/declarations).

Translation units updated

Several .cpp files had been leaning on those headers' transitive using. They now declare what they need locally:

  • Specific using std::...; declarations added to init.cpp and the system/*.cpp printing/formatting code.
  • Point qualification (std::vector, std::chrono::time_point) in calc_tables.cpp, solve_board.cpp, timer.cpp, TestTimer.cpp.
  • testcommon.cpp gains using std::left; using std::right; alongside its existing using std:: block.

Coding standard

.github/instructions/cpp.instructions.md gains a dedicated Namespace using section replacing the terse "No using namespace in headers" bullet: headers ban namespace-scope using-directives and using-declarations (class/function-body using and alias-declarations still fine); .cpp files prefer specific using-declarations, with file-scope using namespace permitted but discouraged; never rely on a using supplied by an included header.

Testing

  • bazel build //... — passes
  • bazel test //... — 93/93 pass

🤖 Generated with Claude Code

Closes #357

zzcgumn and others added 2 commits September 2, 2026 09:20
Expand the terse "No using namespace in headers" bullet into a dedicated
"Namespace using" section:
- Headers: ban namespace-scope using-directives and using-declarations;
  class/function-body using and alias-declarations remain allowed.
- .cpp: prefer specific using-declarations; file-scope using namespace is
  permitted but discouraged.
- Never rely on a using supplied transitively by an included header.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012J8hXn6F1s3ybUogy85sCH
Header code no longer relies on `using namespace std;` (or a namespace-scope
`using std::chrono::time_point;`); the affected declarations are now fully
qualified. Cleaned headers:

- system/system.hpp, system/thread_mgr.hpp, system/time_stat.hpp,
  system/timer.hpp
- tests/TestTimer.hpp

Translation units that had been leaning on those headers' transitive `using`
now declare what they need locally: specific `using std::...;` declarations in
the system/*.cpp and init.cpp printing code, and point qualification of
`std::vector` / `std::chrono::time_point` elsewhere.

Full `bazel build //...` and `bazel test //...` (93 tests) pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012J8hXn6F1s3ybUogy85sCH
@zzcgumn zzcgumn self-assigned this Sep 2, 2026
@zzcgumn
zzcgumn requested review from krtschil and tameware and a lite review from Copilot September 2, 2026 08:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

library/src/system/timer.hpp uses clock_t but does not include <ctime>, which can break header self-containment/portability across standard libraries.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR removes header-level namespace pollution by eliminating using namespace std; and namespace-scope using X::name; from several headers, then updating call sites and the C++ style guide to prevent recurrence.

Changes:

  • Fully qualified standard-library types in affected headers (e.g., std::string, std::vector, std::chrono::time_point).
  • Updated translation units/tests that previously relied on transitive using from headers by adding local using std::...; or explicit std:: qualification.
  • Expanded .github/instructions/cpp.instructions.md with a dedicated “Namespace using” section clarifying header vs .cpp rules.
File summaries
File Description
library/src/system/system.hpp Removes header using namespace std; and qualifies STL usage in declarations.
library/src/system/thread_mgr.hpp Removes header using namespace std; and qualifies std::vector/std::string.
library/src/system/time_stat.hpp Removes header using namespace std; and qualifies std::string return types.
library/src/system/timer.hpp Removes header using namespace std; / using std::chrono::time_point; and qualifies std::string/std::chrono::time_point.
library/tests/TestTimer.hpp Removes namespace-scope using std::chrono::time_point; and fully qualifies std::chrono::time_point.
library/src/init.cpp Adds local using std::...; declarations to avoid relying on header-provided using.
library/src/solve_board.cpp Qualifies std::vector in helper signature to avoid transitive using.
library/src/calc_tables.cpp Qualifies std::vector in helper signature to avoid transitive using.
library/src/system/system.cpp Adds local using std::...; declarations for formatting/containers used in the TU.
library/src/system/thread_mgr.cpp Adds local using std::...; declarations used by output/formatting code.
library/src/system/time_stat.cpp Adds local using std::...; declarations used by formatting code.
library/src/system/time_stat_list.cpp Adds local using std::...; declarations used by formatting code.
library/src/system/timer.cpp Adds local using std::...; declarations and qualifies chrono types where needed.
library/src/system/timer_group.cpp Adds local using std::...; declarations used by formatting/string building.
library/src/system/timer_list.cpp Adds local using std::...; declarations used by printing code.
library/tests/TestTimer.cpp Qualifies std::chrono::time_point in end() to avoid relying on header using.
library/tests/testcommon.cpp Adds using std::left/right; alongside existing local using block.
.github/instructions/cpp.instructions.md Documents the “Namespace using” policy in detail.
Review details
  • Files reviewed: 18/18 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread library/src/system/timer.hpp
- timer.hpp: include <ctime> explicitly for clock_t (member syst0) instead
  of relying on a transitive include via <chrono> (Copilot review)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012J8hXn6F1s3ybUogy85sCH

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes consistently remove header-level namespace pollution while updating dependent translation units accordingly, and the updates are low-risk and stylistically aligned with the documented conventions.

Review details
  • Files reviewed: 18/18 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@krtschil krtschil left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go

…ild)

The DDS_SCHEDULER-only Scheduler::PrintTiming() used unqualified string,
ofstream, setw, setprecision and fixed, which had resolved via a transitive
`using namespace std;` from the now-cleaned system headers. Add
function-local using-declarations so the scheduler=true build compiles again.

Verified: `bazel build --define=scheduler=true //library/src:dds`,
`--define=ab_stats=true`, and `bazel build/test //...` all pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012J8hXn6F1s3ybUogy85sCH

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

A few headers/translation units still rely on unqualified clock_t/clock() (not guaranteed outside std::), which can cause non-portable compile failures after removing header-level using.

Review details

Suppressed comments (5)

Previously missed (5) — in code that hasn't changed since the last review.

library/tests/TestTimer.cpp:97

  • Use std::clock_t / std::clock() consistently in this block as well (and update the static_cast type) to avoid depending on a global clock_t name.
  std::chrono::time_point<Clock> user1 = Clock::now();
  clock_t sys1 = clock();

  duration<double, std::milli> d = user1 - user0_;
  const long tuser = static_cast<long>(d.count());

library/src/system/timer.cpp:66

  • Prefer std::clock_t/std::clock() here so this .cpp does not rely on a global clock_t or clock() being injected via C headers; this becomes more important after removing transitive using from headers.
    library/src/system/timer.hpp:38
  • After removing header-level using namespace std, clock_t should be qualified as std::clock_t for standard compliance; <ctime> only guarantees std::clock_t, not a global clock_t typedef on all platforms.
    library/tests/TestTimer.cpp:93
  • This file now avoids relying on transitive using from headers; clock_t and clock() should be written as std::clock_t / std::clock() for standard compliance (including <ctime> only guarantees these names in std::).
    library/tests/TestTimer.hpp:47
  • This header now avoids namespace-scope using declarations; clock_t should similarly be written as std::clock_t because <ctime> only guarantees the typedef in std::, not necessarily in the global namespace.
  • Files reviewed: 19/19 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot review 5088220782: <ctime> only guarantees clock_t/clock() in
namespace std, not the global namespace. After removing header-level
`using`, write them qualified.

- timer.hpp / timer.cpp: std::clock_t member, std::clock() calls
- TestTimer.hpp / TestTimer.cpp: std::clock_t (member, param, casts),
  std::clock() calls
- test_timer_test.cpp: same, for module consistency

bazel build //... , --define=scheduler=true //library/src:dds , and
bazel test //... (93 tests) all pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012J8hXn6F1s3ybUogy85sCH

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes consistently remove header-level namespace pollution and update dependent translation units accordingly without introducing API/behavior changes.

Review details
  • Files reviewed: 20/20 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@zzcgumn zzcgumn added the Clean Copilot review Copilot reviewed and had neither new comments nor new suppressed comments. label Sep 2, 2026
@zzcgumn
zzcgumn merged commit 0f19070 into develop Sep 2, 2026
13 of 14 checks passed
@zzcgumn
zzcgumn deleted the fix/removing_using_std_from_headers branch September 2, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Clean Copilot review Copilot reviewed and had neither new comments nor new suppressed comments.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove using std from header files.

3 participants