std::as_const (3) - Linux Manuals
std::as_const: std::as_const
Command to display std::as_const
manual in Linux: $ man 3 std::as_const
NAME
std::as_const - std::as_const
Synopsis
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
Possible implementation
template <class T>
constexpr std::add_const_t<T>& as_const(T& t) noexcept
{
return t;
}
Example
// Run this code
#include <string>
#include <cassert>
#include <utility>
#include <type_traits>
int main()
{
std::string mutableString = "Hello World!";
const std::string& constView = std::as_const(mutableString);
assert( &constView == &mutableString );
assert( &std::as_const( mutableString ) == &mutableString );
using WhatTypeIsIt = std::remove_reference_t<decltype(std::as_const(mutableString))>;
static_assert(std::is_same<std::remove_const_t<WhatTypeIsIt>, std::string>::value,
"WhatTypeIsIt should be some kind of string." );
static_assert(!std::is_same< WhatTypeIsIt, std::string >::value,
"WhatTypeIsIt shouldn't be a mutable string." );
}
See also
is_const checks if a type is const-qualified
(class template)
(C++11)
add_cv
add_const
add_volatile adds const or/and volatile specifiers to the given type
(class template)
(C++11)
(C++11)
(C++11)
remove_cv
remove_const
remove_volatile removes const or/and volatile specifiers from the given type
(class template)
(C++11)
(C++11)
(C++11)
Pages related to std::as_const
- std::as_bytes,std::as_writable_bytes (3) - std::as_bytes,std::as_writable_bytes
- std::asctime (3) - std::asctime
- std::asin(std::complex) (3) - std::asin(std::complex)
- std::asin(std::valarray) (3) - std::asin(std::valarray)
- std::asin,std::asinf,std::asinl (3) - std::asin,std::asinf,std::asinl
- std::asinh(std::complex) (3) - std::asinh(std::complex)
- std::asinh,std::asinhf,std::asinhl (3) - std::asinh,std::asinhf,std::asinhl
- std::assoc_laguerre,std::assoc_laguerref,std::assoc_laguerrel (3) - std::assoc_laguerre,std::assoc_laguerref,std::assoc_laguerrel
- std::assoc_legendre,std::assoc_legendref,std::assoc_legendrel (3) - std::assoc_legendre,std::assoc_legendref,std::assoc_legendrel
- std::assume_aligned (3) - std::assume_aligned
- std::async (3) - std::async