On this page
std::add_cv, std::add_const, std::add_volatile
Defined in header <type_traits> |
||
---|---|---|
|
(1) | (since C++11) |
|
(2) | (since C++11) |
|
(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
The behavior of a program that adds specializations for any of the templates described on this page is undefined.
Member types
Name | Definition |
---|---|
type |
the type T with the cv-qualifier |
Helper types
|
(since C++14) | |
|
(since C++14) | |
|
(since C++14) |
Possible implementation
|
Notes
These transformation traits can be used to establish non-deduced contexts in template argument deduction:
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
#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
(C++11)
|
checks if a type is const-qualified (class template) |
(C++11)
|
checks if a type is volatile-qualified (class template) |
(C++11)(C++11)(C++11)
|
removes const and/or volatile specifiers from the given type (class template) |
(C++17)
|
obtains a reference to const to its argument (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/add_cv