Namespaces
Variants

std::meta::size_of

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Reflection queries
Reflection layout queries
Type properties
Type property queries
 
Defined in header <meta>
consteval std::size_t size_of( std::meta::info r );
(since C++26)

Returns the size of the type or (sub)object represented by r.

Parameters

r - a reflection value

Return value

If r represents a type “reference to T”, returns sizeof(T*).

Otherwise, if r represents a type U, returns sizeof(U).

Otherwise, equivalent to std::meta::size_of(std::meta::type_of(r)).

Exceptions

Throws std::meta::exception unless all of the following conditions are met:

  • std::meta::dealias(r) represents one of the following:
  • If std::meta::dealias(r) represents a type, std::meta::is_complete_type(r) is true.

Notes

This function treats “reference of T” the same as “pointer to T”. This matches the representation of non-static reference members.

The result is non-zero even if r represents a direct base class relationship of an empty base class.

Example

#include <meta>

static_assert(std::meta::size_of(^^int) == sizeof(int));
static_assert(std::meta::size_of(^^int*) == sizeof(int*));

consteval bool test()
{
    struct A; 
    // constexpr auto _ = std::meta::size_of(^^A);
        // error: uncaught exception of type 'std::meta::exception';
        // 'what()': 'reflection with incomplete type in size_of'
    return true;
}
static_assert(test());

struct B { int i, j; };
static_assert(sizeof(B) == std::meta::size_of(^^B));

template<class T>
struct C { T m; };
static_assert(size_of(^^C<char>) == sizeof(char));

struct D { int x : 015; };
static_assert(size_of(^^D) == sizeof(int));

// static_assert(size_of(^^D::x) == sizeof(int));
    // error: uncaught exception of type 'std::meta::exception';
    // 'what()': 'reflection not suitable for size_of'

int main() {}

See also

returns the alignment of the reflected object or type
(function) [edit]
returns the size in bits of the reflected object or type
(function) [edit]