C++ Logo

SG12

Advanced search

Subject: Re: Missing non-void return on some paths
From: Jonathan Wakely (cxx_at_[hidden])
Date: 2021-05-08 16:23:48


On Sat, 8 May 2021, 21:52 JF Bastien via SG12, <sg12_at_[hidden]>
wrote:

>
> I don’t think that’s something I’d propose. It seems better to mandate
> that all paths clearly return, or be annotated with no return, unreachable,
> throw, or (list of similar terminators).
>
> Specifying data flow analysis seems like something we wouldn’t want to
> standardize.
>

I agree. In the f4 example x is not a constant expression. It would be a
huge change to require data flow analysis for non-constants, and decide if
a function was well-formed based on that. It would be inconsistent with:

void f5(int x) {
  assert(x==3);
  int a[x];
}

It might be reasonable to require it for constant expressions e.g.

int foo() {
  static_assert(CHAR_BIT==8);
  const int n = sizeof(int);
  if (n > 4) return 0;
  if (n > 1) return 1;
}

However, those conditions could use if constexpr, and then the compiler
already does the right thing.

> So the error would be not as smart as could be, but it would just forbid
> overly clever code.
>

And if you want to be clever, there should be a simple annotation you can
add to tell the compiler to trust you. Like std::unreachable.

>



SG12 list run by sg12-owner@lists.isocpp.org