次の方法で共有


add_cv クラス

型から const volatile 型を作成します。

構文

template <class T>
struct add_cv;

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

パラメーター

T
変更する型。

解説

変更後の型 add_cv<T> のインスタンスには、add_volatileadd_const の両方で変更された T と同等の type メンバー typedef があります。ただし、T が CV 修飾子を既に持っている場合、参照の場合、または関数の場合は除きます。

add_cv_t<T> ヘルパー型は、add_cv<T> メンバー typedef type にアクセスするショートカットです。

// 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()

必要条件

ヘッダー: <type_traits>

名前空間: std

関連項目

<type_traits>
remove_const クラス
remove_volatile クラス