Namespaces
Variants

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

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
add_cv add_const add_volatile
(C++11) (C++11) (C++11)
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < class T >
struct add_cv ;
(1) (C++11부터)
template < class T >
struct add_const ;
(2) (C++11부터)
template < class T >
struct add_volatile ;
(3) (C++11부터)

type 멤버 typedef를 제공합니다. 이는 T 와 동일하지만, ( T 가 함수, 참조 또는 이미 해당 cv-qualifier를 가지고 있는 경우를 제외하고) cv-qualifier가 추가됩니다.

1) 양쪽 모두에 const volatile 을 추가합니다
2) 추가 const
3) 추가 volatile

프로그램이 이 페이지에 설명된 템플릿들 중 어느 하나에 대해 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

멤버 타입

이름 정의
type cv 한정자를 가진 T 타입

헬퍼 타입

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

가능한 구현

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

참고 사항

이러한 변환 특성들은 템플릿 인자 추론에서 비추론 영역(non-deduced contexts) 을 설정하는 데 사용될 수 있습니다:

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); // 오류: 'T'에 대해 충돌하는 타입을 추론함
g(4.2, 0); // OK: g<double> 호출

예제

#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();
}

출력:

Non-cv
Const
Volatile
Const-volatile

참고 항목

(C++11)
타입이 const 한정자인지 확인합니다
(클래스 템플릿)
타입이 volatile 한정자인지 확인합니다
(클래스 템플릿)
주어진 타입에서 const 및/또는 volatile 지정자를 제거합니다
(클래스 템플릿)
(C++17)
인자에 대한 const 참조를 얻습니다
(함수 템플릿)