C++ Logo

std-proposals

Advanced search

Re: [std-proposals] constexpr tolower, toupper, isalpha

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 3 Jul 2025 11:07:53 +0200
Il 03/07/25 10:50, Frederick Virchanza Gotham via Std-Proposals ha scritto:
> Granted I didn't have to write that out myself because ChatGPT did it
> for me, but can we make the likes of std::tolower, std::toupper,
> std::isalpha, and so on, all constexpr?

While you were at it, why didn't you ask ChatGPT why?


> In C++ why aren't tolower(), toupper(), isalpha() etc. constexpr
> functions? Can they be made constexpr?

> In C++, functions like |tolower()|, |toupper()|, |isalpha()|, etc.,
> from |<cctype>| are *not |constexpr|* for the following reasons:
>
>
> 1. *Legacy C Origins*
>
> These functions come from the C standard library (|ctype.h|), where
> |constexpr| does not exist. When C++ adopted them, they were kept
> compatible with the C standard, which includes not just syntax but
> also semantics. Changing them to |constexpr| would violate strict
> compatibility, particularly in C++ implementations that must remain
> ABI-compatible with C libraries.
>
>
> 2. *Locale Dependency*
>
> These functions are affected by the *current C locale*, which is a
> *runtime global state* (set by |setlocale()|). This means their
> behavior can change at runtime depending on the program's locale
> configuration. |constexpr| functions must be *deterministic and
> independent of runtime state* — a condition these functions fail to meet.
>
> This dependence on locale makes compile-time evaluation unsafe and
> non-portable.
>
> ------------------------------------------------------------------------
>
>
> Can They Be Made |constexpr|?
>
> *Yes, but only under certain constraints.*
>
> If you *reimplement them in a locale-independent way*, using only
> compile-time constant expressions, then yes — you can make your own
> |constexpr| versions.
>
>
> Example: |constexpr_isalpha()|
>
> |constexpr bool is_alpha(char c) { return (c >= 'A' && c <= 'Z') || (c
> >= 'a' && c <= 'z'); } |
>
> This is compile-time safe because it doesn’t depend on runtime locale
> or external state.
>
> Similarly:
>
> |constexpr char to_upper(char c) { return (c >= 'a' && c <= 'z') ? (c
> - 'a' + 'A') : c; } |
>
> These versions work only for *ASCII*, and not for *full Unicode* or
> *locale-aware* behavior.
>
> ------------------------------------------------------------------------
>
>
> Summary
>
> *
>
> Standard |tolower()| et al. are *not |constexpr|* due to:
>
> o
>
> C standard compatibility
>
> o
>
> Locale dependence (runtime state)
>
> *
>
> They *cannot be made |constexpr|* without breaking standard semantics.
>
> *
>
> You *can write your own |constexpr| versions*, but they will be
> limited to *ASCII-only* and *locale-independent behavior*.
>
> This is a classic case where language/library design prioritizes
> backward compatibility and runtime configurability over compile-time
> optimization.
>

Can we please stop with this stream of ideas that have zero research
behind, or no motivation whatsoever?

It has effectively killed the purpose of this list, and I suspect the
participation too.


--
Giuseppe D'Angelo

Received on 2025-07-03 09:07:55