Date: Tue, 2 Sep 2025 15:38:54 +0200
wt., 2 wrz 2025 o 14:49 David Brown via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On 02/09/2025 14:24, Hans Åberg via Std-Proposals wrote:
> >
> >
> >> On 2 Sep 2025, at 14:14, Jan Schultke <janschultke_at_[hidden]> wrote:
> >>
> >> You seem to be confusing some mostly unrelated concepts.
> >>
> >>>> 1. C does not allow _BitInt(1); should C++ to make generic programming
> >>>> more comfortable?
> >>>
> >>> The ring ℤ/2ℤ of integers modulo 2, also a field, is isomorphic to the Boolean ring 𝔹 having exclusive or as addition and logical conjunction as multiplication.
> >>>
> >>> If bool 1+1 is defined to 0, then it is already in C++.
> >>
> >> Whether there is some other C++ thing that works mathematically the
> >> same doesn't say anything about whether _BitInt(1) is valid or should
> >> be valid. The issue is regarding a specific type.
> >
> > It could have been defined to be the same as bool.
>
> No, it could not. _BitInt(1), if it is to exist, has the two values -1
> and 0. Like all signed integer types, arithmetic overflow on it is
> undefined, and like all _BitInt types, there is no integer promotion.
> Thus for _BitInt(1), (-1) + (-1) is UB.
>
Do we need UB here? This is not `int` and we could have all operations
defined. What would we gain from it?
> unsigned _BitInt(1) can hold 0 and 1, has no integer promotion, and has
> modulo arithmetic behaviour - thus 1 + 1 gives 0 for "unsigned _BitInt(1)".
>
> Both signed _BitInt(1) and unsigned _BitInt(1) are integer types
> representing numbers. In C23 (and presumably C++ when it gets _BitInt),
> these are included in the "standard integer types". "bool", on the
> other hand, is /not/ a standard integer type in C++ (though it is one in C).
>
> Being able to represent two distinct values does not mean a type behaves
> like "bool".
>
> >
> >>> However, in GCC and Clang, bool 1+1 = 1, and I could not see what the standard specifies.
> >>
> >> Any nonzero integer is true when converted to bool, so "bool(true +
> >> true)" is essentially "1 + 1 != 0". bool gets promoted to int before
> >> any operation.
> >
> > Apparently, there is no operator+ for bool. I did:
> > bool a = 1, b = 1, c = a + b;
> >
>
> Correct. There is also no "+" operator for char, or short int - other
> than _BitInt, types smaller than "int" get promoted to "int" before the
> operation.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On 02/09/2025 14:24, Hans Åberg via Std-Proposals wrote:
> >
> >
> >> On 2 Sep 2025, at 14:14, Jan Schultke <janschultke_at_[hidden]> wrote:
> >>
> >> You seem to be confusing some mostly unrelated concepts.
> >>
> >>>> 1. C does not allow _BitInt(1); should C++ to make generic programming
> >>>> more comfortable?
> >>>
> >>> The ring ℤ/2ℤ of integers modulo 2, also a field, is isomorphic to the Boolean ring 𝔹 having exclusive or as addition and logical conjunction as multiplication.
> >>>
> >>> If bool 1+1 is defined to 0, then it is already in C++.
> >>
> >> Whether there is some other C++ thing that works mathematically the
> >> same doesn't say anything about whether _BitInt(1) is valid or should
> >> be valid. The issue is regarding a specific type.
> >
> > It could have been defined to be the same as bool.
>
> No, it could not. _BitInt(1), if it is to exist, has the two values -1
> and 0. Like all signed integer types, arithmetic overflow on it is
> undefined, and like all _BitInt types, there is no integer promotion.
> Thus for _BitInt(1), (-1) + (-1) is UB.
>
Do we need UB here? This is not `int` and we could have all operations
defined. What would we gain from it?
> unsigned _BitInt(1) can hold 0 and 1, has no integer promotion, and has
> modulo arithmetic behaviour - thus 1 + 1 gives 0 for "unsigned _BitInt(1)".
>
> Both signed _BitInt(1) and unsigned _BitInt(1) are integer types
> representing numbers. In C23 (and presumably C++ when it gets _BitInt),
> these are included in the "standard integer types". "bool", on the
> other hand, is /not/ a standard integer type in C++ (though it is one in C).
>
> Being able to represent two distinct values does not mean a type behaves
> like "bool".
>
> >
> >>> However, in GCC and Clang, bool 1+1 = 1, and I could not see what the standard specifies.
> >>
> >> Any nonzero integer is true when converted to bool, so "bool(true +
> >> true)" is essentially "1 + 1 != 0". bool gets promoted to int before
> >> any operation.
> >
> > Apparently, there is no operator+ for bool. I did:
> > bool a = 1, b = 1, c = a + b;
> >
>
> Correct. There is also no "+" operator for char, or short int - other
> than _BitInt, types smaller than "int" get promoted to "int" before the
> operation.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-09-02 13:39:13