Hacker News new | ask | show | jobs
by yakubin 2020 days ago
You can use "if constexpr" for that.
2 comments

In this case this doesn't work because "if constexpr" parses both branches. If the new library version changes function signatures, the if-branch for the old library version produces an error:

https://www.godbolt.org/z/4KK997

PS: interesting to note that Zig does the "right thing":

https://www.godbolt.org/z/9c13PY

There was a proposal to do the correct thing and copy D's static if, but it was rejected for fairly contrived reasons IIRC.

Andrei Alexandrescu mentions it in a talk, if constexpr doesn't really do much of anything useful because it introduces a scope.

Even though one or other branch of the if-statement won't be valid C++? How does it know that set_color is a function if it isn't defined anywhere?