Date: Tue, 4 Mar 2025 09:43:48 +0100
Hi Tymi,
for the definition of pure:
May a pure function only call other pure functions or is it enough, if the other function has no side effects?
f(int i)
{
if (i == 1)
side_effect();
}
f(0) would have no side effect, but f as a whole is not pure.
Can a pure function call f(0)? Would the compiler know, if f is in another translation unit, which parameter combinations make it pure?
const parameters are no guarantee that the parameters are not changed (const_cast). Of course you can disallow it for a pure function.
If it is disallowed to modify them anyway, do the parameters absolutely have to be const?
const member functions can have side effects. mutable member variables, global variables, non-const references back to member functions. How do you handle that? Is it allowed? Can a mutable member variable, which is just used for caching, be modified inside a pure function? That is the difference between a logical and a physical definition of const.
May the compiler assume that pure functions always return the same value for the same parameters? Can those functions be run at compile-time? Can the compiler create a look-up table? Should some of those optimizations be guided? E.g. typical parameters, which should be pre-computed at compile-time?
-----Ursprüngliche Nachricht-----
Von:Tymi via Std-Proposals <std-proposals_at_[hidden]>
Gesendet:Di 04.03.2025 08:07
Betreff:[std-proposals] [[pure]] attribute
An:std-proposals_at_[hidden];
CC:Tymi <tymi.cpp_at_[hidden]>;
Some functions have no side effects, and while they can be often optimised, the compiler is not aware of that.
Consider this example:
```cpp
template <std::arithmetic T> constexpr T(const T a, const T b) { return a + b; }
```
That function could be declared as pure, or in other words, produces no side effects.
I think this is a great way to optimise frequent calls, such as a pure function's result can be cached for the same arguments provided.
Diagnostics: if a pure function has side effects, the compiler shall issue a warning and calling that function is undefined.
pure member function shall be declared as const, otherwise, the program is ill-formed.
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-03-04 08:48:37