C++ Logo

std-proposals

Advanced search

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

From: JJ Marr <jjmarr_at_[hidden]>
Date: Tue, 4 Mar 2025 02:15:47 -0500
Shouldn't it be "ill-formed, no diagnostic required"? If compilers are
required to emit a warning when [[pure]] is misapplied, then we cannot use
[[pure]] when the compiler is unable to determine that the function is pure
(because otherwise the compiler might fail to emit a warning when one is
required). That would defeat the purpose of this proposal.

On Tue, Mar 4, 2025, 2:07 a.m. 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
>

Received on 2025-03-04 07:16:00