std::add_cv...dd_volatile(3)
π NAME π
Okay bestie, welcome to std::add_cv...dd_volatile(3) from the C++ Standard Libary manual. The NAME is:
std::add_cv,std::add_const,std::add_volatile -
std::add_cv,std::add_const,std::add_volatile
So thatβs the trio: std::add_cv, std::add_const, and std::add_volatile. Slay. π
β¨ Synopsis β¨
Defined in header <type_traits>
template< class T > (1) (since C++11)
struct add_cv;
template< class T > (2) (since C++11)
struct add_const;
template< class T > (3) (since C++11)
struct add_volatile;
These provide the member typedef type which is the same as T, except it has a cv-qualifier added β unless T is a function, a reference, or already has this cv-qualifier. Periodt.
- adds both
constandvolatile - adds
const - adds
volatile
β οΈ If the program adds specializations for any of the templates described on this page, the behavior is undefined. Like, do not do that, bestie. π
π«Ά Member types π«Ά
typeβ the typeTwith the cv-qualifier.
π Helper types π
template< class T > (since C++14)
using add_cv_t = typename add_cv<T>::type;
template< class T > (since C++14)
using add_const_t = typename add_const<T>::type;
template< class T > (since C++14)
using add_volatile_t = typename add_volatile<T>::type;
π Possible implementation π
template<class T> struct add_cv { typedef const volatile T type; };
template<class T> struct add_const { typedef const T type; };
template<class T> struct add_volatile { typedef volatile T type; };
πΈ Notes πΈ
These transformation traits can be used to establish non-deduced contexts in template argument deduction. Itβs giving βdeduction but make it controlled,β honestly.
template<class T>
void f(const T&, const T&);
template<class T>
void g(const T&, std::add_const_t<T>&);
f(4.2, 0); // error, deduced conflicting types for 'T'
g(4.2, 0); // OK, calls g<double>
π¦ Example π¦
// Run this code
#include <iostream>
#include <type_traits>
struct foo
{
void m() { std::cout << "Non-cv\n"; }
void m() const { std::cout << "Const\n"; }
void m() volatile { std::cout << "Volatile\n"; }
void m() const volatile { std::cout << "Const-volatile\n"; }
};
int main()
{
foo{}.m();
std::add_const<foo>::type{}.m();
std::add_volatile<foo>::type{}.m();
std::add_cv<foo>::type{}.m();
}
Output π
Non-cv
Const
Volatile
Const-volatile
π See also π
is_constβ checks if a type is const-qualified(C++11)(class template)is_volatileβ checks if a type is volatile-qualified(C++11)(class template)remove_cv,remove_const,remove_volatileβ removes const and/or volatile specifiers from the given type(C++11)(class template)(C++11)(C++11)as_constβ obtains a reference to const to its argument(C++17)(function template)
Okay thatβs the whole page, bestie. Signing off from http://cppreference.com β version/date 2024.06.10 β page std::add_cv...dd_volatile(3). Stay cv-qualified and iconic πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms