Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      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_cvadd_constadd_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
       
      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 typedeftype which is the same asT, except it has a cv-qualifier added (unlessT is a function, a reference, or already has this cv-qualifier)

      1) adds bothconst andvolatile
      2) addsconst
      3) addsvolatile

      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 typeT 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<class T>struct add_cv{typedefconstvolatile T type;}; template<class T>struct add_const{typedefconst T type;}; template<class T>struct add_volatile{typedefvolatile T type;};

      [edit]Notes

      These transformation traits can be used to establishnon-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>

      [edit]Example

      Run this code
      #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()constvolatile{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-cvConstVolatileConst-volatile

      [edit]See also

      (C++11)
      checks if a type is const-qualified
      (class template)[edit]
      checks if a type is volatile-qualified
      (class template)[edit]
      removesconst and/orvolatile specifiers from the given type
      (class template)[edit]
      (C++17)
      obtains a reference toconst to its argument
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/add_cv&oldid=139238"

      [8]ページ先頭

      ©2009-2025 Movatter.jp