3 ms·
You can use "if constexpr" for that.
by yakubin 6y ago
You can use "if constexpr" for that.
- rwmj 6y agoEven 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?
- flohofwoe 6y agoIn 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 https://www.godbolt.org/z/4KK997 PS: interesting to note that Zig does the "right thing": https://www.godbolt.org/z/9c13PY https://www.godbolt.org/z/9c13PY
- mhh__ 6y agoThere 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.