GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::as_const(3) C++ Standard Libary std::as_const(3)

std::as_const - std::as_const


Defined in header <utility>
template< class T > (1) (since C++17)
constexpr std::add_const_t<T>& as_const( T& t ) noexcept;
template< class T > (2) (since C++17)
void as_const( const T&& ) = delete;


1) Forms lvalue reference to const type of t.
2) const rvalue reference overload is deleted to disallow rvalue arguments.


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


Feature-test macro: __cpp_lib_as_const

// Run this code


#include <string>
#include <cassert>
#include <utility>
#include <type_traits>


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." );
}


is_const checks if a type is const-qualified
(C++11) (class template)
add_cv
add_const
add_volatile adds const or/and volatile specifiers to the given type
(C++11) (class template)
(C++11)
(C++11)
remove_cv
remove_const
remove_volatile removes const or/and volatile specifiers from the given type
(C++11) (class template)
(C++11)
(C++11)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.