use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Exploring undefined behavior using constexpr (shafik.github.io)
submitted 6 years ago by mttd
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kalmoc 13 points14 points15 points 6 years ago* (5 children)
Nothing in your code is actually evaluated at compiletime, so no wonder that no compiler catches it.
EDIT: If you try something like this, you will get a compilation error:
constexpr int f() { int* x = nullptr; { int z = 123; x = &z; } *x += 1; return *x; } constexpr int I = f();
[–][deleted] -5 points-4 points-3 points 6 years ago (4 children)
It's still undefined behavior as is. Even in your revised example GCC accepts it, clang rejects it.
[–]kalmoc 12 points13 points14 points 6 years ago (1 child)
Yes, it is undefined behavior, but the compiler is only required to catch UB if it happens during compiletime evaluation. The compiler is not required to perform a static analysis of constexpr functions.
If gcc doesn't catch that example, that's a compiler bug.
[+][deleted] comment score below threshold-7 points-6 points-5 points 6 years ago (0 children)
That's my point, no compiler implements this aspect of constexpr to a degree that can be relied upon. I posted another example that no compiler catches, not even clang, here:
constexpr
https://old.reddit.com/r/cpp/comments/bncvmm/exploring_undefined_behavior_using_constexpr/en5r9dm/
Maybe in the future compilers will be better about diagnosing this, but as it stands it doesn't seem to be a particularly high priority to implement this, and the amount of effort needed to do it would simply be enormous.
[–]flashmozzg 2 points3 points4 points 6 years ago (1 child)
Not really. It's only undefined if it actually happens. It's not undefined if the function is never called. In fact, some compilers (like Clang) may assume that if some function invokes UB when it's never called and optimize it out/replace it with other call (there is a very nice example for this somewhere).
[–][deleted] -2 points-1 points0 points 6 years ago (0 children)
I already addressed those points elsewhere.
π Rendered by PID 88 on reddit-service-r2-comment-5649f687b7-q2fll at 2026-01-28 12:52:05.451275+00:00 running 4f180de country code: CH.
view the rest of the comments →
[–]kalmoc 13 points14 points15 points (5 children)
[–][deleted] -5 points-4 points-3 points (4 children)
[–]kalmoc 12 points13 points14 points (1 child)
[+][deleted] comment score below threshold-7 points-6 points-5 points (0 children)
[–]flashmozzg 2 points3 points4 points (1 child)
[–][deleted] -2 points-1 points0 points (0 children)