On Mon, 5 Oct 2026 at 13:07, Tomasz Kaminski <[email protected]> wrote:
>
>
>
> On Mon, Oct 5, 2026 at 1:37 PM Jonathan Wakely <[email protected]> wrote:
>>
>> This avoids pedantic warnings in C++98 caused by %zu in the format
>> strings passed to the __throw_out_of_range_fmt function:
>>
>> include/bitset:845: warning: ISO C++98 does not support the 'z' gnu_printf
>> length modifier [-Wformat=]
>>
>> The definition of the function is in src/c++11/functexcept.cc and
>> compiled using -std=gnu++11 so the %zu format works fine. We just can't
>> tell C++98 translation units that it works, so suppress the attribute in
>> C++98.
>>
>> libstdc++-v3/ChangeLog:
>>
>> * include/bits/stdexcept_throwfwd.h (__throw_out_of_range_fmt):
>> Do not use __format__ attribute for C++98.
>> ---
>>
>> Tested x86_64-linux.
>
> I was going to ask if silencing the warning about the unsupported modifier
> wouldn't be better,
No, because we would need to do it at every call site, not only at the
declaration where the attribute is present. That would be horribly
noisy:
void
_M_range_check(size_type __n) const
{
#if __cplusplus < 201103L
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat"
#endif
if (__n >= this->size())
__throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
"(which is %zu)>= this->size() "
"(which is %zu)"),
__n, this->size());
#if __cplusplus < 201103L
# pragma GCC diagnostic pop
#endif
}
and then repeat that in vector, and bitset, and string.
> but that would also disable all warnings emitted by this attribute, which
> gives behavior equivalent
> to removing it.
And objectively worse in every way :-)
> Could you add a line about this in commit description.
> LGTM.
>>
>>
>> libstdc++-v3/include/bits/stdexcept_throwfwd.h | 6 +++++-
>> 1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/libstdc++-v3/include/bits/stdexcept_throwfwd.h
>> b/libstdc++-v3/include/bits/stdexcept_throwfwd.h
>> index 9b0aad3571d3..3980a60bd5c3 100644
>> --- a/libstdc++-v3/include/bits/stdexcept_throwfwd.h
>> +++ b/libstdc++-v3/include/bits/stdexcept_throwfwd.h
>> @@ -81,9 +81,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>> void
>> __throw_out_of_range(const char*) __attribute__((__noreturn__,__cold__));
>>
>> +#if __cplusplus >= 201103L
>> + // C++98 does not support %zu which is used in most calls to this
>> function.
>> + [[__gnu__::__format__(__gnu_printf__, 1, 2)]]
>> +#endif
>> void
>> __throw_out_of_range_fmt(const char*, ...)
>> - __attribute__((__noreturn__,__cold__,__format__(__gnu_printf__, 1, 2)));
>> + __attribute__((__noreturn__,__cold__));
>>
>> void
>> __throw_runtime_error(const char*) __attribute__((__noreturn__,__cold__));
>> --
>> 2.55.0
>>