C++ Logo

sg12

Advanced search

Re: [SG12] Missing non-void return on some paths

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sat, 8 May 2021 22:23:48 +0100
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.



>

Received on 2021-05-08 16:24:08