Spec-Zone.ru › C++

std::meta::size_of

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:
    • type
    • object
    • (scalar) value
    • variable of non-reference type
    • non-static data member that is not a bit-field
    • direct base class relationship
    • data member description whose bit width is ⊥
  • 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

alignment_of
(C++26)
returns the alignment of the reflected object or type
(function)
bit_size_of
(C++26)
returns the size in bits of the reflected object or type
(function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/meta/size_of

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API