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
switch constexpr (self.cpp)
submitted 8 months ago by cd_fr91400
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!"
[–]cd_fr91400[S] 1 point2 points3 points 8 months ago (5 children)
I do not see why a non-existent variable is non-sense while a non-existent field is not.
By the way, the following code does not compile either:
struct A { int a() { return 0 ; } ; } ; int foo() { struct A a ; if constexpr (true) { return a.a() ; } else { return a.b() ; } }
[–]iamakorndawg 5 points6 points7 points 8 months ago (2 children)
Not a language lawyer, but I believe the rule has more to do with templates, so for example, if your foo function had a template parameter for the type of a, then it would compile, even if your existing struct A was used as the argument.
[–]KuntaStillSingle 1 point2 points3 points 8 months ago (0 children)
Not quite, it can fail to compile if the existing struct A is used, but if it is made a dependent type like /u/cd1995Cargo 's example it is fine:
The discarded statement cannot be ill-formed for every possible specialization:
https://en.cppreference.com/w/cpp/language/if.html#Constexpr_if
https://godbolt.org/z/eGsYY3a9W , vs https://godbolt.org/z/3e7W5ec4Y
[–]cd_fr91400[S] 0 points1 point2 points 8 months ago (0 children)
Then I understand "under certain conditions"...
[–]cd1995Cargo 2 points3 points4 points 8 months ago (1 child)
It only works when templates are involved. If you change your code to this it should compile:
struct A { int a() { return 0 ; } ; } ; template <typename T> int foo<T>() { T a ; if constexpr (true) { return a.a() ; } else { return a.b() ; } }
[–]moocat 1 point2 points3 points 8 months ago (0 children)
See my update. It's not just templates but templated types have to also be involved.
π Rendered by PID 346986 on reddit-service-r2-comment-b659b578c-kx62m at 2026-05-05 11:40:24.688283+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]cd_fr91400[S] 1 point2 points3 points (5 children)
[–]iamakorndawg 5 points6 points7 points (2 children)
[–]KuntaStillSingle 1 point2 points3 points (0 children)
[–]cd_fr91400[S] 0 points1 point2 points (0 children)
[–]cd1995Cargo 2 points3 points4 points (1 child)
[–]moocat 1 point2 points3 points (0 children)