Converting some "if" statements to "if constexpr"
Hi,
This is not something that I’ve noticed, but rather my IDE (VS 2019). A lot of if
statements could be converted to if constexpr
, which I assume would make runtime (ever so slightly) faster.
Examples:
- https://bitbucket.org/blaze-lib/blaze/src/f8445b46a7ac1590d9760977055413d6134fbf85/blaze/math/expressions/DMatDMatMultExpr.h#lines-628
- https://bitbucket.org/blaze-lib/blaze/src/f8445b46a7ac1590d9760977055413d6134fbf85/blaze/math/expressions/DMatDMatMultExpr.h#lines-639
- https://bitbucket.org/blaze-lib/blaze/src/f8445b46a7ac1590d9760977055413d6134fbf85/blaze/math/dense/DynamicMatrix.h#lines-998
- https://bitbucket.org/blaze-lib/blaze/src/f8445b46a7ac1590d9760977055413d6134fbf85/blaze/math/dense/CustomMatrix.h#lines-789
- etc.
Or maybe there is a reason why it’s beneficial to keep them as they are?
Comments (4)
-
-
- changed status to duplicate
Duplicate of #282.
-
reporter Thanks for the quick response.
Just curious, isn’t this a feature that could easily be handled via macros? Similar to other keywords like
BLAZE_ALWAYS_INLINE
. So for example:#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L # define BLAZE_CONSTEXPR constexpr #else # define BLAZE_CONSTEXPR #endif
and then
if BLAZE_CONSTEXPR ( PF && IsVectorizable_v<Type> ) { // ... }
That way it is compatible with both
C++14
andC++17
at the same time. -
Hi Phil!
You are correct, for these simple cases a macro would be sufficient. However, that is the boring cases that don’t really exploit the real power of
if constexpr
and don’t really improve the code (other than getting rid of the warnings).if constexpr
is primarily interesting for the cases where I can collapse several SFINAE-guarded functions into one. That problem unfortunately cannot be solved by a macro because the resulting code wouldn’t work in C++14 (e.g. conflicting return types, etc.).Best regards,
Klaus!
- Log in to comment
Hi Phil!
You are correct, there is a lot of opportunity to use
if constexpr
. So far Blaze does not use anyif constexpr
. The reason is that Blaze is a C++14 library throughout the 3.x releases (see also the “Compiler Compatibility” section on the main Blaze page) andif constexpr
is a C++17 feature. Usingif constexpr
would therefore be a breaking change for many Blaze users (especially some major companies). We will, however, move to C++17 with the 4.0 release and with that will be able to useif constexpr
.This issue is a duplicate to issue #282. Therefore I’ll close this one as duplicate and keep issue #282 as a reminder. Thanks,
Best regards,
Klaus!