#include <ugmisc/member.hpp>
Public Member Functions | |
| template<class... T> | |
| constexpr auto | operator() (T &&...objs) const |
Static Public Member Functions | |
| template<class... T> | |
| static constexpr auto | with (T &&...objs) |
Static Public Attributes | |
| static constexpr member_value< Access, MEMBER_COPY_AUTO > | copy_auto {} |
| static constexpr member_value< Access, MEMBER_COPY_ALWAYS > | copy_always {} |
| static constexpr member_value< Access, MEMBER_COPY_NEVER > | copy_never {} |
| Access | A type declared using UGMISC_DECL_MEMBER_ACCESS. |
| CopyValue | Determines whether the object obtained by calling member_value(objects...) will contain a copy of the found member value, rather than a reference. The default, MEMBER_COPY_AUTO, will return a copy if doing so is trivial and the member could not be modified through the reference. |
A template which obtains a named member from one of a sequence of objects, or a fallback default value.
Normally, a reference is returned, but there are two cases in which a value will be copied.
The above member_value expressions evaluate to the same reference, which is the first of the following which is valid:
If neither of the listed expressions is valid, neither is the get() call, and compilation will fail.
This yields the first of:
Consider this example:
This should work, assuming the type of x.foo can be copied in a constant expression. But this might not:
The value of x_foo is a small object containing a reference to x.foo. Because x.foo is not static (or might not be; Thing::x might be static but let's assume it isn't), we can't make x_foo and its reference to x.foo constexpr.
But sometimes we only wanted a copy. We can always revert to creating the object and calling get() in a single expression, but sometimes that is not necessary.
If x.foo can't be modified through its reference anyway, and is trivially copyable, and is not volatile, then x_foo will contain a copy instead of a reference. This behaviour can be changed by passing either MEMBER_COPY_ALWAYS or MEMBER_COPY_NEVER as the second template argument. Alternatively the static member objects copy_auto, copy_always and copy_never may be more convenient.
|
inlinestaticconstexpr |
| objs | Objects to try calling. |
|
staticconstexpr |
An instance of member_value with the same Access template argument, and a CopyValue argument of MEMBER_COPY_ALWAYS.
This will copy the found member object rather than referencing it.
|
staticconstexpr |
An instance of member_value with the same Access template argument, and a CopyValue argument of MEMBER_COPY_AUTO. This will copy the found object rather than referencing it iff the object is not volatile, is constant, and is trivially copyable.
|
staticconstexpr |
An instance of member_value with the same Access template argument, and a CopyValue argument of MEMBER_COPY_NEVER.
This will always reference a found member object rather than copying it.