Skip to content

Replace C-style casts with static_cast/reinterpret_cast for -Wold-style-cast compliance#445

Open
PavelGuzenfeld wants to merge 2 commits intocameron314:masterfrom
PavelGuzenfeld:feature/fix-c-style-casts
Open

Replace C-style casts with static_cast/reinterpret_cast for -Wold-style-cast compliance#445
PavelGuzenfeld wants to merge 2 commits intocameron314:masterfrom
PavelGuzenfeld:feature/fix-c-style-casts

Conversation

@PavelGuzenfeld
Copy link

@PavelGuzenfeld PavelGuzenfeld commented Mar 10, 2026

Summary

Fixes #444

Replace all C-style casts with static_cast / reinterpret_cast across the three public headers, enabling compilation with -Wold-style-cast -Wuseless-cast -Werror on GCC 14/15.

  • lightweightsemaphore.h (5 casts): (time_t)static_cast<time_t>, (long)static_cast<long>, (std::uint64_t)static_cast<std::uint64_t>, (int)static_cast<int>
  • blockingconcurrentqueue.h (15 casts): All (size_t), (ssize_t), (int), (LightweightSemaphore::ssize_t)static_cast<>. Assert (BlockingConcurrentQueue*)1reinterpret_cast<>
  • concurrentqueue.h (1 cast): Suppress -Wuseless-cast around hash_thread_id — the static_cast<size_t> is needed for 32-bit platforms but redundant on 64-bit

Test plan

  • GCC 15.2.0 with -std=c++23 -Wall -Wextra -Wpedantic -Werror -Wold-style-cast -Wuseless-cast -Wconversion -Wsign-conversion — clean
  • GCC 14.2.0 with same flags — clean
  • C++17, C++20, C++23 — all clean
  • No behavioral changes — purely mechanical cast replacements

Test results

All tests executed inside Docker (gcc:latest, GCC 15.2.0):

Unit tests (make tests && bin/unittests)

All tests passed:

  • create_empty_queue, enqueue_one, enqueue_many, enqueue_and_dequeue, enqueue_dequeue_threaded
  • token_move, blocking_wrappers, c_api_create, c_api_enqueue, c_api_destroy
  • acquire_and_signal, try_acquire_and_signal, core_add_only_list, core_thread_local
  • core_free_list, core_spmc_hash, explicit_strings_threaded, large_traits

Fuzz tests (bin/fuzztests)

700+ iterations executed, 0 failures:

Test Passed Failed
multithread_produce 122 0
multithread_consume 114 0
multithread_produce_and_consume 132 0
completely_random 110 0
core_add_only_list 102 0
core_thread_local 124 0

…le-cast compliance

Projects using -Wold-style-cast and -Wuseless-cast (common in modern C++ codebases
with strict warning policies) cannot compile concurrentqueue without warnings.

Changes:
- lightweightsemaphore.h: Replace C-style casts with static_cast in timed_wait,
  waitWithPartialSpinning, waitManyWithPartialSpinning, and signal methods
- blockingconcurrentqueue.h: Replace C-style casts with static_cast for
  sema->signal, sema->waitMany, sema->tryWaitMany, sema->availableApprox calls
  and constructor MAX_SEMA_SPINS; replace C-style pointer casts in assert with
  reinterpret_cast
- concurrentqueue.h: Suppress -Wuseless-cast around hash_thread_id static_cast
  that is needed for cross-platform correctness but redundant on 64-bit

Tested clean with GCC 14/15, C++17/20/23, using -Wall -Wextra -Wpedantic -Werror
-Wold-style-cast -Wuseless-cast -Wconversion -Wsign-conversion and other strict flags.
Copy link
Owner

@cameron314 cameron314 left a comment

Choose a reason for hiding this comment

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

Personally, I think the code is less readable when using static_cast for integral conversions. However, it's probably more important to avoid warnings.

{
if ((details::likely)(inner.enqueue_bulk(std::forward<It>(itemFirst), count))) {
sema->signal((LightweightSemaphore::ssize_t)(ssize_t)count);
sema->signal(static_cast<LightweightSemaphore::ssize_t>(count));
Copy link
Owner

Choose a reason for hiding this comment

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

This is not equivalent when sizeof(BlockingConcurrentQueue::ssize_t) != sizeof(LightweightSemaphore::ssize_t).

If (ssize_t)count is not negative (and it shouldn't be here!) then this works. But I'd still prefer an assert rather than an implicit assumption.

Copy link
Author

@PavelGuzenfeld PavelGuzenfeld Mar 15, 2026

Choose a reason for hiding this comment

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

I've restored the two-step cast through ssize_t and added assert(static_cast<ssize_t>(count) >= 0) before each conversion. Applied consistently across all 10 call sites in the file.

…ith assert

Address maintainer review: restore the intermediate ssize_t cast that
was dropped when converting from C-style casts, and add an explicit
assert that the value is non-negative to guard against size differences
between BlockingConcurrentQueue::ssize_t and LightweightSemaphore::ssize_t.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C-style casts break compilation with -Wold-style-cast -Werror (GCC 14/15, C++23)

2 participants