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

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Primary type categories
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
add_cvadd_constadd_volatile
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Type transformations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type trait constants
 
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

[edit] Member types

Name Definition
type the type T with the cv-qualifier

[edit] 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)

[edit] Possible implementation

template< class T >
struct add_cv {
    typedef typename std::add_volatile<typename std::add_const<T>::type>::type type;
};
 
template< class T> struct add_const { typedef const T type; };
 
template< class T> struct add_volatile { typedef volatile T type; };

[edit] Example

#include <iostream>
#include <type_traits>
 
struct foo
{
    void m() { std::cout << "Non-cv\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

[edit] See also

(C++11)
checks if a type is const-qualified
(class template)
(C++11)
checks if a type is volatile-qualified
(class template)
removes const or/and volatile specifiers from the given type
(class template)