C ++ compile-time cleanliness checks?

Is it possible to specify the purity check time in C ++ mode?

i.e:.

this function does not read from anything other than it arguments this function does not write to anything; it only returns the return value 
+4
source share
2 answers

const-correctness , and high levels of compiler warnings should do a lot of what you ask. Also specify a very strict modern C ++ dialect for the compiler (which can annoy you when you use third-party libraries and code that do not match)

If not, then there are many static analysis tools, some of which are open source, some expensive, such as Coverity, Parasoft C ++ Test, etc.

+4
source

Although there is no portable way to do this, gcc implements function attributes that may be close to what interests you. attributes you should check:

pure - Many functions have no effects other than a return value, and their return value depends only on parameters and / or global variables. Such a function may be subject to the general exclusion of subexpression and loop optimization, as is the case with the arithmetic operator. These functions must be declared with the pure attribute.

and

const - many functions do not check values ​​other than their arguments, and have no effects other than the return value. This is basically a slightly more strict class than the pure attribute below, since functions are not allowed to read global memory.

You specify the attribute as part of the prototype:

 int square (int x) __attribute__ ((const)); int square (int x) { return x * x; } 
+2
source

Source: https://habr.com/ru/post/1304312/


All Articles