Date: Wed, 10 Sep 2025 20:11:44 +0200
On 9/10/25 16:59, Howard Hinnant via Std-Proposals wrote:
> On Sep 10, 2025, at 6:10 AM, Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> If a C library has something like:
>>
>> void foo(_BitInt(16) x);
>>
>> , then it can plausibly have documentation and example code with foo(42) or foo(some_int) in it. It can be confusing and add friction when the same C library has subtly different interfaces from C and from C++.
>
>
> Good example!
>
> But I see it from the other side of the the fence. I think it would be a good thing if foo(some_int) did not compile when switching from C to C++. I.e. switch from C to C++ and you can catch some run-time UB at compile-time. That looks like a good safety feature to me.
>
> foo(42) is another matter. I’d love for the compiler to be smart enough to recognize that the compile-time value is safe to narrow.
We do support that love in another context where we detect and reject narrowing:
int i = 0;
unsigned int x[] = { 0, // ok, because constant
i }; // error: narrowing
If we wish to make certain initializations (e.g. copy-initialization of
certain types) narrowing-detecting, we have precedent for that.
Jens
> On Sep 10, 2025, at 6:10 AM, Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> If a C library has something like:
>>
>> void foo(_BitInt(16) x);
>>
>> , then it can plausibly have documentation and example code with foo(42) or foo(some_int) in it. It can be confusing and add friction when the same C library has subtly different interfaces from C and from C++.
>
>
> Good example!
>
> But I see it from the other side of the the fence. I think it would be a good thing if foo(some_int) did not compile when switching from C to C++. I.e. switch from C to C++ and you can catch some run-time UB at compile-time. That looks like a good safety feature to me.
>
> foo(42) is another matter. I’d love for the compiler to be smart enough to recognize that the compile-time value is safe to narrow.
We do support that love in another context where we detect and reject narrowing:
int i = 0;
unsigned int x[] = { 0, // ok, because constant
i }; // error: narrowing
If we wish to make certain initializations (e.g. copy-initialization of
certain types) narrowing-detecting, we have precedent for that.
Jens
Received on 2025-09-10 18:11:50