Spec-Zone.ru › C++

std::meta::add_const, std::meta::add_volatile, std::meta::add_cv

Defined in header <meta>
consteval std::meta::info add_const( std::meta::info r );
(1) (since C++26)
consteval std::meta::info add_volatile( std::meta::info r );
(2) (since C++26)
consteval std::meta::info add_cv( std::meta::info r );
(3) (since C++26)

Returns a reflection of a type T, represented by r, with the cv-qualifier added. If r represents a function, a reference, or already has this qualifier, r is returned unmodified.

1) Adds const qualifier.
Equivalent to return std::meta::dealias(^^std::add_const_t<T>);.
2) Adds volatile qualifier.
Equivalent to return std::meta::dealias(^^std::add_volatile_t<T>);.
3) Adds both const and volatile qualifiers.
Equivalent to return std::meta::dealias(^^std::add_cv_t<T>);.

Parameters

r - a reflection value

Return value

A reflection of the type represented by r with added topmost cv-qualifiers appropriately.

Exceptions

Throws std::meta::exception if r does not represent a type or type alias.

Example

#include <meta>
#include <print>
#include <source_location>

static_assert
(""
    and (add_const(^^int) == ^^const int)
    and (add_volatile(^^int) == ^^volatile int)
    and (add_cv(^^int) == ^^const volatile int)
    and (add_cv(^^const volatile int) == ^^const volatile int)
    // add_cv only works on types, not on pointers
    and (add_cv(^^int*) != ^^const volatile int*)
    and (add_cv(^^int*) == ^^int* const volatile)
    and (add_cv(^^const volatile int*) == ^^const volatile int* const volatile)
);

inline void fun_name(std::source_location src = std::source_location::current())
{
    return std::println("{}", src.function_name());
}

struct S
{
    void fun() { fun_name(); }
    void fun() const { fun_name(); }
    void fun() volatile { fun_name(); }
    void fun() const volatile { fun_name(); }
};

int main()
{
    S{}.fun();
    typename [:add_const(^^S):]{}.fun();
    typename [:add_volatile(^^S):]{}.fun();
    typename [:add_cv(^^S):]{}.fun();
}

Possible output:

void S::fun()
void S::fun() const
void S::fun() volatile
void S::fun() const volatile

See also

is_const
(C++26)
checks if reflection represents a const type or a function type with const qualifier
(function)
is_volatile
(C++26)
checks if reflection represents a volatile type or a function type with volatile qualifier
(function)
remove_cvremove_constremove_volatile
(C++26)(C++26)(C++26)
removes const and/or volatile qualifiers from reflected type
(function)
as_const
(C++17)
obtains a reference to const to its argument
(function template)
ranges::as_const_viewviews::as_const
(C++23)
converts a view into a constant_range
(class template) (range adaptor object)
add_cvadd_constadd_volatile
(C++11)(C++11)(C++11)
adds const and/or volatile qualifiers to the given type
(class template)

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

Spec-Zone.ru

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