TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 227 subscribers
Post #24026 18
Could staticassert handle non-constant values in the future?

In the future, could static
assert be used as a static analysis utility to check the correctness of code, including non-constant values?

As a simple example, the code

int x = 10;
staticassert(x > 5);

would compile without error, because at that point, 'x' is indeed greater than 5.

This could be expanded to "trace back" values to determine if they are programmatically guaranteed to meet some condition. In the examples below, func1 and func2 will compile without error, but func3 will create a compiler error because there's no guarantee that 's' is not NULL.

void stringStuff(const char* s){
static
assert(s);
// ...etc...
}

void func1(){ // Good
char s10;
stringStuff(s);
}

void func3(){ // Good
char s = malloc(100);
if(s){
stringStuff(s);
}
}

void func2(){ // Compiler Error
char
s = malloc(100);
stringStuff(s);
}

https://redd.it/1o303bd
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 14, 2026I tested whether a coding agent would use C++23 ranges without being told Blog post: https…
  2. Oct 14, 2026C++ futures part 1: The mental model https://devblogs.microsoft.com/oldnewthing/20261012-0…
  3. Oct 14, 2026C++ futures part 2: The mental model for std::promise, std::packaged_task https://devblogs…
  4. Oct 12, 2026[Release] slick-logger v2.2.0 — Lock-free asynchronous C++20 logging with multi-process sh…
  5. Oct 12, 2026Cheap alternative to reboot parameters (nohz, rcu, isolcpus) for stable benchmarks When no…
  6. Oct 12, 2026This C++ drama is crazy - Adobe votes no https://www.youtube.com/watch?v=DEqx_NMMH9Q https…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →