]> www.ginac.de Git - ginac.git/commitdiff
Avoid "expression with side effects..." warning using ex_to<T>().
authorRichard Kreckel <kreckel@ginac.de>
Mon, 25 Nov 2024 23:21:28 +0000 (00:21 +0100)
committerRichard Kreckel <kreckel@ginac.de>
Mon, 25 Nov 2024 23:33:32 +0000 (00:33 +0100)
Clang++ warns that instantiations of this inline template function
ex_to<T>(const ex &) are "expression with side effects [that] will be
evaluated despite being used as an operand to 'typeid'".

Let's declare this ex_to<T>() as a pure function (i.e. having no
side effects) for compilers which support __attribute__((__pure__)).

GCC seems to support this attribute since more than ten years.

ginac/compiler.h
ginac/ex.h

index e9682794d05a67d3e9f0bc3007f374902b8d4a42..0216b681345589f86d53574d3c0290e9d9f31339 100644 (file)
 #define unlikely(cond) __builtin_expect((cond), 0)
 #define likely(cond) __builtin_expect((cond), 1)
 #define attribute_deprecated __attribute__ ((deprecated))
+#define attribute_pure __attribute__((__pure__))
 #else
 #define unlikely(cond) (cond)
 #define likely(cond) (cond)
 #define attribute_deprecated
+#define attribute_pure
 #endif
 
 #endif // ndef GINAC_COMPILER_DEP_H
index 5381a38aad85fa2213bfd28ffadf6cacdf496a69..37d74e7bb00dee7398e9c6f10c49825dd8948077 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "basic.h"
 #include "ptr.h"
+#include "compiler.h"
 
 #include <functional>
 #include <iosfwd>
@@ -973,7 +974,7 @@ inline bool is_exactly_a(const ex &obj)
  *  @param e expression
  *  @return reference to object of class T
  *  @see is_exactly_a<class T>() */
-template <class T>
+template <class T> attribute_pure
 inline const T &ex_to(const ex &e)
 {
        GINAC_ASSERT(is_a<T>(e));