std::add_cv, std::add_const, std::add_volatile - cppreference.com (original) (raw)
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)
adds both const and volatile
adds const
adds volatile
If the program adds specializations for any of the templates described on this page, the behavior is undefined.
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 struct add_cv { typedef const volatile T type; }; template struct add_const { typedef const T type; }; template struct add_volatile { typedef volatile T type; };
[edit] Notes
These transformation traits can be used to establish non-deduced contexts in template argument deduction:
template void f(const T&, const T&); template void g(const T&, std::add_const_t&); f(4.2, 0); // error, deduced conflicting types for 'T' g(4.2, 0); // OK, calls g
[edit] Example
#include #include 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::type{}.m(); std::add_volatile::type{}.m(); std::add_cv::type{}.m(); }
Output:
Non-cv Const Volatile Const-volatile