Replace C-style casts with static_cast/reinterpret_cast for -Wold-style-cast compliance#445
Open
PavelGuzenfeld wants to merge 2 commits intocameron314:masterfrom
Open
Conversation
…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.
cameron314
approved these changes
Mar 14, 2026
Owner
cameron314
left a comment
There was a problem hiding this comment.
Personally, I think the code is less readable when using static_cast for integral conversions. However, it's probably more important to avoid warnings.
blockingconcurrentqueue.h
Outdated
| { | ||
| 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)); |
Owner
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #444
Replace all C-style casts with
static_cast/reinterpret_castacross the three public headers, enabling compilation with-Wold-style-cast -Wuseless-cast -Werroron 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*)1→reinterpret_cast<>concurrentqueue.h(1 cast): Suppress-Wuseless-castaroundhash_thread_id— thestatic_cast<size_t>is needed for 32-bit platforms but redundant on 64-bitTest plan
-std=c++23 -Wall -Wextra -Wpedantic -Werror -Wold-style-cast -Wuseless-cast -Wconversion -Wsign-conversion— cleanTest results
All tests executed inside Docker (gcc:latest, GCC 15.2.0):
Unit tests (make tests && bin/unittests)
All tests passed:
Fuzz tests (bin/fuzztests)
700+ iterations executed, 0 failures: