C++ Logo

sg7

Advanced search

Attributes introspection

From: Aurelien Cassagnes <aurelien.cassagnes_at_[hidden]>
Date: Tue, 16 Apr 2024 20:37:01 +0900
[ The conversation started in std-proposal list but after initial feedback
from Andrew, Giuseppe and others, it seems appropriate to continue the
conversation here.
After surveying the P2996R2 paper I believe this is not covered.
I am working on building the draft proposal further but happy to collect
more feedback here while I work on it... ]


Draft for "Attributes introspection"

Attributes are used to great extent and there likely will be attributes
added as the language evolve.
What is missing now is a way for generic code to look into the attributes
related to an entity.
A motivating example is following

#include <functional>

[[nodiscard]] bool foo(int i) { return i % 2 ; }

template <class F, class... Args>
constexpr std::invoke_result_t<F, Args...> logInvoke(F&& f, Args&&... args)
{
// Do some extra work, log the call
// ...
// Fwd call
return std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}

int main() {
foo(0); // Warning on discarded return
logInvoke(foo, 0); // No warning on discarded return
}

Other examples of wrapping around callables can be found, whether by
closure or explicitly registering callbacks for dispatch, etc.
The proposal here is to introduce a `declattr` keyword to look into
attributes bound to an entity, the form would be likely equivalent to how
`decltype` or `declval` are used.

Received on 2024-04-16 11:37:14