C++ Logo

std-proposals

Advanced search

Re: [std-proposals] DR - std::visit non-intuitive behavior + new semantic for C-style variadic functions + pattern matching but better

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sat, 3 Jun 2023 09:14:31 -0400
On Sat, Jun 3, 2023 at 7:09 AM Nikl Kelbon via Std-Proposals <
std-proposals_at_[hidden]> wrote:

>
> And last problem: this MUST compile
> int main() {
> std::variant<int, int> var;
> std::get_if<int>(&var);
> }
>

No comment on the first part, but this part is a complete non-starter. We
do it this way because

    std::variant<time_t, ptrdiff_t> var;
    var = time();
    if (ptrdiff_t *p = std::get_if<ptrdiff_t>(&var)) {
        puts("the variant holds a ptrdiff_t");
    }

This "MUST" either compile and Do The Right Thing (if time_t and ptrdiff_t
are distinguishable types), or else noisily fail to compile (if they are
the same type). What it absolutely must NOT do is silently compile and Do
The Wrong Thing — i.e. permit us to accidentally treat a time as a pointer
offset or vice versa.

–Arthur

Received on 2023-06-03 13:14:45