Namespaces
Variants

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

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Type properties
Type property queries
 
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

(C++26)
checks if reflection represents a const type or a function type with const qualifier
(function) [edit]
checks if reflection represents a volatile type or a function type with volatile qualifier
(function) [edit]
removes const and/or volatile qualifiers from reflected type
(function) [edit]
(C++17)
obtains a reference to const to its argument
(function template) [edit]
converts a view into a constant_range
(class template) (range adaptor object)[edit]
(C++11)(C++11)(C++11)
adds const and/or volatile qualifiers to the given type
(class template) [edit]