ugmisc 0.2-76
Miscellaneous C++ header library
Loading...
Searching...
No Matches
member_value< Access, CopyValue > Struct Template Reference

#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_AUTOcopy_auto {}
static constexpr member_value< Access, MEMBER_COPY_ALWAYScopy_always {}
static constexpr member_value< Access, MEMBER_COPY_NEVERcopy_never {}

Detailed Description

template<class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
struct member_value< Access, CopyValue >
Template Parameters
AccessA type declared using UGMISC_DECL_MEMBER_ACCESS.
CopyValueDetermines 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.

Usage

UGMISC_DECL_MEMBER_ACCESS(GetFoo, foo); // Declares GetFoo.
ugmisc::member_value< GetFoo >::with(x, y).get();
ugmisc::member_value< GetFoo >{}(x, y).get(); // Same as above.
#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
Definition member.hpp:165

The above member_value expressions evaluate to the same reference, which is the first of the following which is valid:

  1. x.foo
  2. y.foo

If neither of the listed expressions is valid, neither is the get() call, and compilation will fail.

Defaults

UGMISC_DECL_MEMBER_ACCESS(GetFoo, foo); // Declares GetFoo.
ugmisc::member_value< GetFoo >{}(x, y).get( sum, 10, 20 );

This yields the first of:

  1. x.foo
  2. y.foo
  3. sum(10, 20)

Being as constexpr as possible

Consider this example:

constexpr Thing x{}; // x.foo exists
constexpr auto fooval = ugmisc::member_value<GetFoo>{}(x).get();

This should work, assuming the type of x.foo can be copied in a constant expression. But this might not:

constexpr Thing x{};
constexpr auto x_foo = ugmisc::member_value<GetFoo>(x); // Might fail.
constexpr auto fooval = x_foo.get();

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.

Member Function Documentation

◆ with()

template<class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
template<class... T>
constexpr auto member_value< Access, CopyValue >::with ( T &&... objs)
inlinestaticconstexpr
Parameters
objsObjects to try calling.

Member Data Documentation

◆ copy_always

template<class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
member_value<Access, MEMBER_COPY_ALWAYS> member_value< Access, CopyValue >::copy_always {}
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.

See also
Being as constexpr as possible

◆ copy_auto

template<class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
member_value<Access, MEMBER_COPY_AUTO> member_value< Access, CopyValue >::copy_auto {}
staticconstexpr
Note
This is the default value of CopyValue so it is rare that copy_auto would be useful.

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.

See also
Being as constexpr as possible

◆ copy_never

template<class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
member_value<Access, MEMBER_COPY_NEVER> member_value< Access, CopyValue >::copy_never {}
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.

See also
Being as constexpr as possible

The documentation for this struct was generated from the following file: