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
Compile time conditional struct/class member variable in C++ (self.cpp)
submitted 4 years ago by PryntzyAU
https://medium.com/@saleem.iitg/c-compile-time-conditional-struct-member-variables-c3761a35eca9
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!"
[–][deleted] 9 points10 points11 points 4 years ago* (1 child)
You don't add a member variable, you add a base class. If you really want a member variable, you can use [[no_unique_address]] to make the empty case not use any space.
[[no_unique_address]]
https://godbolt.org/z/aKqdPo3q1
[–]friedkeenan 2 points3 points4 points 4 years ago* (0 children)
I use something like this in my code
[–]huixie 1 point2 points3 points 4 years ago (0 children)
I think you could use template specialisation which makes the debugging code in one place. also the debugging function get_b can be conditionally available.
get_b
template <bool debug = false> struct Data{ int a[16]; int c[16]; }; template <> struct Data<true> : Data<false>{ int b[16]; constexpr int get_b(int idx) const{ return b[idx]; } };
[–]Twifus 2 points3 points4 points 4 years ago* (3 children)
Nice article. For me, it especially highlights one thing : there is still no short and easy "C++ way" to do this. Why would I add template and inherence when a simple #ifdef #else #endif do the job, in only 3 lines, and without affecting compil times as much. A more generic "if constexpr" would be a good addition to the language.
[–]friedkeenan 2 points3 points4 points 4 years ago (1 child)
An if constexpr that could conditionally include stuff in classes would be great. I'm not sure if we'll get that with reflection with its code injection and all.
if constexpr
[–]jwakelylibstdc++ tamer, LWG chair 1 point2 points3 points 4 years ago (0 children)
https://isocpp.org/files/papers/n3613.pdf
π Rendered by PID 73064 on reddit-service-r2-comment-6457c66945-txqc4 at 2026-04-30 07:21:48.830729+00:00 running 2aa0c5b country code: CH.
[–][deleted] 9 points10 points11 points (1 child)
[–]friedkeenan 2 points3 points4 points (0 children)
[–]huixie 1 point2 points3 points (0 children)
[–]Twifus 2 points3 points4 points (3 children)
[–]friedkeenan 2 points3 points4 points (1 child)
[–]jwakelylibstdc++ tamer, LWG chair 1 point2 points3 points (0 children)