Skip to content

Conversation

StephanTLavavej
Copy link
Member

@StephanTLavavej StephanTLavavej commented May 27, 2025

@Codiferous is implementing WG21-P2280R4 in MSVC. With this Defect Report implemented, MSVC emits its (extremely annoying) warning C4127 "conditional expression is constant" for vector<bool>::max_size(), because we marked a local variable as const and that causes "trial evaluation" to happen. With WG21-P2280R4, _Ints_max is recognized as a compile-time constant, causing the if-statement below to emit the warning.

Unfortunately, the underlying vector's max_size() has to be implemented in terms of the allocator's max_size(), and there's no requirement that it be a compile-time constant. So we can't just boil this whole thing down to an if constexpr.

To avoid this, we can extract the condition into a const bool and add a comment why. C4127 has a special case to avoid warnings when a single variable is being tested.

(I am somewhat tempted to globally suppress C4127 in STL headers, but it hasn't been quite enough of a nuisance to resort to that... yet.)

@StephanTLavavej StephanTLavavej requested a review from a team as a code owner May 27, 2025 18:00
@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label May 27, 2025
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews May 27, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in STL Code Reviews May 27, 2025
@AlexGuteniev
Copy link
Contributor

Can the usual way work: extract condition to const bool and then test that bool ? If so, this would avoid bloating debug codegen

@StephanTLavavej
Copy link
Member Author

I believe that extracting the condition to a const bool would avoid the warning, since there's a special case to avoid emitting it for if (variable), but that wouldn't allow us to upgrade the branch to if constexpr, because (AFAICT) there is no actual requirement that a user allocator has a constexpr max_size() (and pre-C++11 user allocators wouldn't).

@AlexGuteniev
Copy link
Contributor

but that wouldn't allow us to upgrade the branch to if constexpr

I think it is still both cleaner way to avoid the warning, at it is almost idiomatic way to do so, and better debug codegen

@StephanTLavavej
Copy link
Member Author

I've pushed that change, since I agree that it's clearer. However, I don't see how this will affect debug codegen.

Copy link
Member

@davidmrdavid davidmrdavid left a comment

Choose a reason for hiding this comment

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

LGTM, soft suggestion to improve comment

@StephanTLavavej StephanTLavavej moved this from Final Review to Ready To Merge in STL Code Reviews May 27, 2025
@AlexGuteniev
Copy link
Contributor

However, I don't see how this will affect debug codegen.

Please see: https://godbolt.org/z/exKhPY5KP

@AlexGuteniev
Copy link
Contributor

However, I don't see how this will affect debug codegen.

Please see: https://godbolt.org/z/exKhPY5KP

Yes I was wrong. The codegen is worse!

@frederick-vs-ja
Copy link
Contributor

Per WG21-P2280R4, IIUC we can determine whether the max_size returns an instance-unrelated constant and use if constexpr in some cases. But such determination is a bit arcane.

@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews May 28, 2025
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit c83fbfd into microsoft:main May 28, 2025
48 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews May 28, 2025
@StephanTLavavej StephanTLavavej deleted the scalar-vector-tensor branch May 28, 2025 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants