C++ Logo

std-proposals

Advanced search

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

From: Tymi <tymi.cpp_at_[hidden]>
Date: Tue, 4 Mar 2025 13:41:14 +0100
Also, maybe there could be traits for them? I'm not sure about that one but
I think it could be useful:
std::is_pure_v

Also optional parameter to the pure attribute as a boolean:
[[pure(std::is_pure_v<decltype(MyFunction)]]
Not sure about this one as well, maybe explicit [[pure(false)]] for non
pure functions :)

On Tue, 4 Mar 2025, 08:07 Tymi, <tymi.cpp_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.
>
>

Received on 2025-03-04 12:41:31