Date: Tue, 29 Apr 2025 19:06:09 +0000
On Tuesday, April 29th, 2025 at 3:43 AM, Aurelien Cassagnes via Std-Proposals <std-proposals_at_[hidden]> wrote:
> There is a fairly arbitrary (unless I missed an important argument) limitation on initialization clause for if statements.
>
> - I have that
> if (auto it = vec.begin(); it != vec.end()) {
>
> - I want this
> if (auto it = vec.begin() /*and*/ auto itt = ++vec.begin(); it != vec.end() && it != itt) {
>
> So you end up using one of the workarounds, which are somewhat hacky imo :/
The particular example can use multi-declarator in
the declaration as https://lists.isocpp.org/std-proposals/2025/04/13529.php
showed. If the two initializers can deduce to
different types, then
if (auto [it, itt] = std::tuple{ v1.begin(), std::next(v1.begin()) };
it != v1.end() && itt != v1.end()) {
To push this idea further, you can do this in C++26
template <class It1, class It2, class It3 = decltype(std::next(std::declval<It1>()))> struct first_last_next{
It1 first;
It2 last;
It3 next = std::next(first);
explicit constexpr operator bool() const noexcept
{
return first != last and first != next;
}
};
if (auto [it, _, itt] = first_last_next{v1.begin(), v1.end()}) {
(* just a contrived snippet to show the possibility;std::next doesn't return an iterator of a different type. *)
> There is a fairly arbitrary (unless I missed an important argument) limitation on initialization clause for if statements.
>
> - I have that
> if (auto it = vec.begin(); it != vec.end()) {
>
> - I want this
> if (auto it = vec.begin() /*and*/ auto itt = ++vec.begin(); it != vec.end() && it != itt) {
>
> So you end up using one of the workarounds, which are somewhat hacky imo :/
The particular example can use multi-declarator in
the declaration as https://lists.isocpp.org/std-proposals/2025/04/13529.php
showed. If the two initializers can deduce to
different types, then
if (auto [it, itt] = std::tuple{ v1.begin(), std::next(v1.begin()) };
it != v1.end() && itt != v1.end()) {
To push this idea further, you can do this in C++26
template <class It1, class It2, class It3 = decltype(std::next(std::declval<It1>()))> struct first_last_next{
It1 first;
It2 last;
It3 next = std::next(first);
explicit constexpr operator bool() const noexcept
{
return first != last and first != next;
}
};
if (auto [it, _, itt] = first_last_next{v1.begin(), v1.end()}) {
(* just a contrived snippet to show the possibility;std::next doesn't return an iterator of a different type. *)
-- Zhihao Yuan, ID lichray The best way to predict the future is to invent it. _______________________________________________ > I'd like to release that restriction and have for example > if ({ > auto it = vec.begin(); > auto itt = ++vec.begin(); > }; it != vec.end() && it != itt) { > > which can be desugared (desugarified ?) to e.g. > { > auto it = vec.begin(); > auto itt = vec.begin(); > if (it != vec.end()) { > } > } > > Colleague pointed me to a GCC expression statements extension which ?maybe? makes `if ({` ambiguous to parse, so I would have to look into that too… > > Is there any current proposal that aims to lift that restriction ? > Cheers. > > Sent from Gmail Mobile
Received on 2025-04-29 19:06:17