Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::as_const

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
      Defined in header<utility>
      template<class T>
      constexprstd::add_const_t<T>& as_const( T& t)noexcept;
      (1)(since C++17)
      template<class T>
      void as_const(const T&&)= delete;
      (2)(since C++17)
      1) Forms lvalue reference to const type oft.
      2) const rvalue reference overload is deleted to disallow rvalue arguments.

      Contents

      [edit]Possible implementation

      template<class T>constexprstd::add_const_t<T>& as_const(T& t)noexcept{return t;}

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_as_const201510L(C++17)std::as_const

      [edit]Example

      Run this code
      #include <cassert>#include <string>#include <type_traits>#include <utility> int main(){std::string mutableString="Hello World!";auto&& constRef= std::as_const(mutableString);     mutableString.clear();// OK//  constRef.clear(); // Error: 'constRef' is 'const' qualified,//        but 'clear' is not marked const assert(&constRef==&mutableString);assert(&std::as_const(mutableString)==&mutableString); using ExprType=std::remove_reference_t<decltype(std::as_const(mutableString))>;     static_assert(std::is_same_v<std::remove_const_t<ExprType>,std::string>,"ExprType should be some kind of string.");    static_assert(!std::is_same_v<ExprType,std::string>,"ExprType shouldn't be a mutable string.");}

      [edit]See also

      (C++11)
      checks if a type is const-qualified
      (class template)[edit]
      (C++11)(C++11)(C++11)
      addsconst and/orvolatile specifiers to the given type
      (class template)[edit]
      removesconst and/orvolatile specifiers from the given type
      (class template)[edit]
      converts aview into aconstant_range
      (class template)(range adaptor object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/as_const&oldid=182820"

      [8]ページ先頭

      ©2009-2025 Movatter.jp