add_cv Class

Makes const volatile type from type.

Syntax

template <class T>
struct add_cv;

template <class T>
using add_cv_t = typename add_cv<T>::type;

Parameters

T
The type to modify.

Remarks

An instance of the modified type add_cv<T> has a type member typedef equivalent to T modified by both add_volatile and add_const, unless T already has the cv-qualifiers, is a reference, or is a function.

The add_cv_t<T> helper type is a shortcut to access the add_cv<T> member typedef type.

Example

// add_cv.cpp
// compile by using: cl /EHsc /W4 add_cv.cpp
#include <type_traits>
#include <iostream>

struct S {
    void f() {
        std::cout << "invoked non-cv-qualified S.f()" << std::endl;
    }
    void f() const {
        std::cout << "invoked const S.f()" << std::endl;
    }
    void f() volatile {
        std::cout << "invoked volatile S.f()" << std::endl;
    }
    void f() const volatile {
        std::cout << "invoked const volatile S.f()" << std::endl;
    }
};

template <class T>
void invoke() {
    T t;
    ((T *)&t)->f();
}

int main()
{
    invoke<S>();
    invoke<std::add_const<S>::type>();
    invoke<std::add_volatile<S>::type>();
    invoke<std::add_cv<S>::type>();
}
invoked non-cv-qualified S.f()
invoked const S.f()
invoked volatile S.f()
invoked const volatile S.f()

Requirements

Header: <type_traits>

Namespace: std

See also

<type_traits>
remove_const Class
remove_volatile Class