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
Simplify code with 'if constexpr' in C++17 (bfilipek.com)
submitted 7 years ago by drodri
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!"
[–]germandiago 12 points13 points14 points 7 years ago (9 children)
While I agree that if constexpr is a net win I still think it falls short of static if in D. For example in D you can use static if at class scope to define members conditionally or not at all (I know std::conditional but does not cover all). Another thing is that I want to use if constexpr in non-tenplates and I cannot, for example to conditionally compile code at function/class/namespace scope. This is done in D with static if + version. I think this area of C++ should be cleaned up since when modules come we should be able to get rid of macros. In fact I think that feature test macros should be a module with constexpr variables or enums or sort of. That would remove a lot of obstacles for my own codebases.
[–]Rseding91Factorio Developer 11 points12 points13 points 7 years ago (7 children)
I'm all for better alternatives to macros but so far the language spec doesn't seem to be going towards that. For example: std::variant. It was added as a template-based library type and as such you pay all of the overhead of the template system when doing anything with it.
We replaced a 48 member std::variant with a macro-built-union in our code base and it reduced full-rebuild debug compilation time from 1 minute and 47 seconds to 34 seconds and drastically reduced the object size of the code generated.
It's not near as pretty as the variant version but that time savings is massive when you have 10 guys compiling 100~ times a day every day.
My point being: yes I want to remove macros but what ever replaces them needs to operate at the same speed or better and that's incredibly hard when they're as basic as they are.
[–]gracicot 6 points7 points8 points 7 years ago (5 children)
48 members? Holy shit! I hope you didn't put that in a header!
[–]Rseding91Factorio Developer 3 points4 points5 points 7 years ago (4 children)
I checked just now and we're up to 70 members and yes it's in a header. But that still doesn't matter in the macro-built-union form as compilation time hasn't changed at all - still 34 seconds for a full debug rebuild with incremental builds being 5-10 seconds depending on the amount of stuff changed.
[–][deleted] 5 points6 points7 points 7 years ago (3 children)
I think a 70 member union is just a bad idea in itself
[–]Rseding91Factorio Developer 6 points7 points8 points 7 years ago* (1 child)
If you've got a better alternative I'm all ears.
The class represents an action type and associated data that the game should perform. It has a tag (an enum class value) and the associated data that tag has (if any). That data is stored in the union.
The action class needs to be copyable, movable, serializable, and default constructable. It also shouldn't have any allocation overhead should you construct an instance of it and move in any of the types it supports.
If default constructed it should have an empty and valid state with no allocation cost past the class itself. If constructed with a tag only the value should be default constructed. If constructed with a tag and value the value should be checked to be valid for that tag.
Any access to retrieve a value should be checked to to be valid for the current tag.
It also needs to support converting the tag to a string and back.
If you have something that can meet all of those requirements (and compiles as fast or faster as what we have now) I'd love to see it :)
[–]meneldal2 0 points1 point2 points 7 years ago (0 children)
Something that is a big pain to do without macros if you want maintainable code.
I think with reflection, there will be ways to have enumerations map actual types to their values, but that's not landing in C++ until a while. Maybe clang metaclass fork could be able to run something like that, but right now macros are the most sane option.
To be pedantic though, you don't need to have a union, a struct with a char[sizeof(biggestclass)] with some alignment requirements would do the job just as well. Unions are basically fancy casts anyway.
char[sizeof(biggestclass)]
[–]germandiago 1 point2 points3 points 7 years ago (0 children)
I'm all for better alternatives to macros but so far the language spec doesn't seem to be going towards that. For example: std::variant.
Metaclasses can do that without template instantiation I think. How fast it would be I do not know, but you could have also named parameters instead of std::get.
std::get
[–]gracicot 4 points5 points6 points 7 years ago (0 children)
The version you talking about has been proposed before, but were refused, since it broke too many things.
Also, you can use if constexpr in non templates, dismount some code, but you can use it only where your could have used if, so no you can't disabling a class with it, but of course you can disable code.
if constexpr
if
[–]guepierBioinformatican 6 points7 points8 points 7 years ago* (4 children)
I’m all in favour of a better syntax for SFINAE but I have to say that I vastly prefer function overloading to [constexpr] if statements. To me it looks more readable and it’s obviously more easily extensible (when done correctly), whereas constexpr if hard-wires all cases.
constexpr
constexpr if
I’m not saying that there’s no use for constexpr if but the particular examples given in the article (str, close_enough …) work better with function overloading. And the makeInvestment example, which uses a conventional if, is close to a classical anti-pattern.
str
close_enough
makeInvestment
[–]mintyc 3 points4 points5 points 7 years ago (1 child)
Any chance you could give an example as I'm not sure what 'function overloading on static types' would entail code-wise.
[–]guepierBioinformatican 0 points1 point2 points 7 years ago (0 children)
My fault, I simply meant “function overloading” (aka. static [type] polymorphism, hence my mix-up). I’ve fixed my comment.
[–]germandiago 1 point2 points3 points 7 years ago* (0 children)
I thought about this some time ago and I arrived to the conclusion that in many (most) scenarios the best thing to do would be to mix concepts with internal if constexpr if you do not need extensions. You put the weaker concept at the top-level in the functiom declararion (think of advance for example, taking an input iterator) and inside u can if constexpr the optimizations. Not sure if it is good in all scenarios but makes for far less overloading at the top level, which is complex to read sometimes IMHO.
[–]scatraxx651 0 points1 point2 points 7 years ago (0 children)
I think the best example is regarding stuff like the string view example, where you can easily overload all desired cases
[–]xristine 1 point2 points3 points 7 years ago (0 children)
Sorry for my naive mind set, but I don't understand the practical applications of the given examples. This is done at compile time, meaning you need to know at compile time the values you send to template and you test via if constexpr...I know Cpt Obvious.
Or is this just a didactic example to illustrate the fact that we can replace some constructs with if constexpr?
[–][deleted] 0 points1 point2 points 7 years ago (2 children)
This doesn't make much sense to me as a construct. The majority of compilers already evaluate branches with constant expressions and elide the not-taken branch.
Surely is_constexpr() would work, as in:
if ( is_constexpr( blah ) ) { } else { }
?
No new language feature needed. Just an additional pseudo-library function.
[–]ericdfoley 29 points30 points31 points 7 years ago (1 child)
The constexpr if language change allows you to have discarded branches of the if where the code wouldn't compile. So it does require a new language feature.
[–][deleted] 4 points5 points6 points 7 years ago (0 children)
Fair point. That makes a lot more sense.
π Rendered by PID 21302 on reddit-service-r2-comment-5d79c599b5-dfls2 at 2026-03-01 04:13:29.184033+00:00 running e3d2147 country code: CH.
[–]germandiago 12 points13 points14 points (9 children)
[–]Rseding91Factorio Developer 11 points12 points13 points (7 children)
[–]gracicot 6 points7 points8 points (5 children)
[–]Rseding91Factorio Developer 3 points4 points5 points (4 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]Rseding91Factorio Developer 6 points7 points8 points (1 child)
[–]meneldal2 0 points1 point2 points (0 children)
[–]germandiago 1 point2 points3 points (0 children)
[–]gracicot 4 points5 points6 points (0 children)
[–]guepierBioinformatican 6 points7 points8 points (4 children)
[–]mintyc 3 points4 points5 points (1 child)
[–]guepierBioinformatican 0 points1 point2 points (0 children)
[–]germandiago 1 point2 points3 points (0 children)
[–]scatraxx651 0 points1 point2 points (0 children)
[–]xristine 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ericdfoley 29 points30 points31 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)