C++ Logo

std-proposals

Advanced search

Re: [std-proposals] [[pure]] attribute

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Wed, 5 Mar 2025 14:09:01 +0100
Can we also have a proven pure function?   =================================   Variant 1: The pure function cannot access any runtime state, only its parameters   The parameters are value only and contain no pointers or references, at least not to outside memory   It would feel like a subset, similar to constexpr / consteval functions   =================================   Variant 2: We define some runtime state handle, and as long as we use that handle, we guarantee that the runtime state exists and is unmodified in regards to the function execution.   E.g. we dynamically load a codepage for string interpretation. But then call the function repeatedly for the same string.     -----Ursprüngliche Nachricht----- Von:Hans Åberg via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 05.03.2025 09:29 Betreff:Re: [std-proposals] [[pure]] attribute An:std-proposals_at_[hidden]; CC:Hans Åberg <haberg_1_at_[hidden]>; Tymi <tymi.cpp_at_[hidden]>; The property pure, depending on how defined, may be undecidable, as in the halting problem. However, it might be useful to be able to tell the optimizer that a function is to be considered semantically pure, even though it may not be in a strict computational sense. This would be similar to the use of the mutable keyword. > On 4 Mar 2025, at 08:07, Tymi via Std-Proposals <std-proposals_at_[hidden]> wrote: > > 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 -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-05 13:13:51