-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update idioms.md with changes to match the code now #4587
Conversation
- Rename TypedInstArgsInfo references to InstLikeTypeInfo. The type was renamed in 07efa02. - Update and correct the link to search for ValueStore (and similar) in the Context class. We must avoid the google-doc-style checks rewriting `repo:` to `repository:` in the URL. - Mention the existance of many types of Store collections now. - Remove pre-C++20 idioms in Field detection. Correct the concept based idioms to work. - Fix code indenting consistency.
@zygoloid Maybe you could review? The concept examples here are based on your changes in toolchain/sem_ir/typed_insts.h, but the derived-from version was something I didn't see in the code yet, so I just put it together on godbolt to make sure it would work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, tiny tweak to the comment update at the end though.
toolchain/docs/idioms.md
Outdated
To detect a field with a specific name with a type derived from a specified base | ||
type, use this idiom: | ||
|
||
```cpp | ||
// HasField<T> is true if T has a `U field` field, | ||
// where `U` extends `BaseClass`. | ||
template <typename T, bool Enabled = true> | ||
inline constexpr bool HasField = false; | ||
template <typename T> | ||
inline constexpr bool HasField< | ||
T, bool(std::is_base_of_v<BaseClass, decltype(T::field)>)> = true; | ||
``` | ||
namespace Internal { | ||
|
||
The equivalent concept is: | ||
template <typename T, typename From> concept FieldDerivedFrom = | ||
std::derived_from<std::remove_cvref_t<T>, From>; | ||
|
||
```cpp | ||
} | ||
|
||
// HasField<T> is true if T has a `U field` field, where `U` extends | ||
// `BaseClass`. | ||
template <typename T> concept HasField = requires (T x) { | ||
{ x.field } -> std::derived_from<BaseClass>; | ||
{ x.field } -> Internal::FieldDerivedFrom<BaseClass>; | ||
}; | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not using this idiom, maybe we should remove it from the documentation for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, especially since it needs a helper. SGTM
Co-authored-by: Richard Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(LG to me, but good to make sure the template stuff LG to Richard as well before merging.)
…4587) - Rename TypedInstArgsInfo references to InstLikeTypeInfo. The type was renamed in 07efa02. - Update and correct the link to search for ValueStore (and similar) in the Context class. We must avoid the google-doc-style checks rewriting `repo:` to `repository:` in the URL. - Mention the existance of many types of Store collections now. - Remove pre-C++20 idioms in Field detection. Correct the concept based idioms to work. - Fix code indenting consistency. --------- Co-authored-by: Richard Smith <[email protected]>
repo:
torepository:
in the URL.