Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | types
Revision as of 01:53, 21 November 2018 by 87.69.233.180 (Talk)

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <type_traits>
template< class T >
struct add_cv;
(1) (since C++11)
template< class T >
struct add_const;
(2) (since C++11)
template< class T >
struct add_volatile;
(3) (since C++11)

Provides 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)

1) adds both const and volatile

2) adds const

3) adds volatile

Contents

Member types

Name Definition
type the type T with the cv-qualifier

Helper types

template< class T >
using add_cv_t       = typename add_cv<T>::type;
(since C++14)
template< class T >
using add_const_t    = typename add_const<T>::type;
(since C++14)
template< class T >
using add_volatile_t = typename add_volatile<T>::type;
(since C++14)

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; };

Example

#include <iostream>
#include <type_traits>
 
struct foo
{
    void m() { std::cout << "Non-const\n"; }
    void m() const { std::cout << "Const\n"; }
};
 
template <class T>
void call_m()
{
    T().m();
}
 
int main()
{
    call_m<foo>();
    call_m<std::add_const<foo>::type>();
}

Output:

Non-cv
Const

See also

(C++11)
checks if a type is const-qualified
(class template) [edit]
checks if a type is volatile-qualified
(class template) [edit]
removes const and/or volatile specifiers from the given type
(class template) [edit]
(C++17)
obtains a reference to const to its argument
(function template) [edit]