News for hackers. Stuff that matters.
Re:C++26: A User-Friednly assert() macro by secondcoming on Saturday March 28, 2026 @05:05PM (#47558113)
Let's not vague post on HN. What's the problem with the above?
Re:C++26: A User-Friednly assert() macro by saagarjha on Saturday March 28, 2026 @08:34PM (#47559363)
The problem is the code unconditionally dereferences the pointer, which would be UB if it was a null pointer. This means it is legal to optimize out any code paths that rely on this, even if they occur earlier in program order.
Re:C++26: A User-Friednly assert() macro by aw1621107 on Saturday March 28, 2026 @08:46PM (#47559439)
> The problem is the code unconditionally dereferences the pointer, which would be UB if it was a null pointer.

Only when NDEBUG is defined, right?

Re:C++26: A User-Friednly assert() macro by saagarjha on Saturday March 28, 2026 @09:03PM (#47559528)
No, the code that does this is always active
Re:C++26: A User-Friednly assert() macro by gottheUIblues on Saturday March 28, 2026 @09:20PM (#47559605)
Right so strictly speaking C++ could do anything here when passed a null pointer, because even though assert terminates the program, the C++ compiler cannot see that, and there is then undefined behaviour in that case
Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:06AM (#47561248)
> because even though assert terminates the program, the C++ compiler cannot see that

I think it should be able to. I'm pretty sure assert is defined to call abort when triggered and abort is tagged with [[noreturn]], so the compiler knows control flow isn't coming back.

Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:03AM (#47561237)
Shouldn't control flow diverge if the assert is triggered when NDEBUG is not defined? Pretty sure assert is defined to call abort when triggered and that is tagged [[noreturn]].
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:19AM (#47561301)
Sorry, yes, I misread you
Re:C++26: A User-Friednly assert() macro by pwdisswordfishy on Sunday March 29, 2026 @02:44AM (#47560903)
But if the assertion fails, the program is aborted before the pointer would have been dereferenced, making it not UB. This explanation is bogus.
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:20AM (#47561306)
Only if the assert is active. It basically means that the code is invalid when NDEBUG is set.
Re:C++26: A User-Friednly assert() macro by quuxplusone on Sunday March 29, 2026 @10:02AM (#47563229)
When NDEBUG is set, there is no test, no assertion, at all. So yes, this code has UB if you set NDEBUG and then pass it a null pointer — but that's obvious. The code does exactly what it looks like it does; there's no tricks or time travel hiding here.
Re:C++26: A User-Friednly assert() macro by teo_zero on Sunday March 29, 2026 @03:23AM (#47561066)
> it is legal to optimize out any code paths that rely on this, even if they occur earlier in program order.

I don't think this is true. The compiler cannot remove or reorder instructions that have a visible effect.

  if (p == 0)
    printf("Ready?\n");
  *p++;
The printf() can't be omitted.
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:18AM (#47561291)
No, this is explicitly legal. Most compilers will shy away from it these days since it made a lot of people upset, but it's definitely allowed.
Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:13AM (#47561275)
> The compiler cannot remove or reorder instructions that have a visible effect.

You might be surprised! When it comes to UB compilers can and do reorder/eliminate instructions with side effects, resulting in "time travel" [0].

IIRC the upcoming version of the C standard bans this behavior, but the C++ standard still allows it (for now, at least).

[0]: https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63...