std::as_const
cppreference.com
<tbody>
</tbody>
template< class T > constexpr std::add_const_t<T>& as_const( T& t ) noexcept; |
(1) | ( C++17) |
template< class T > void as_const( const T&& ) = delete; |
(2) | ( C++17) |
1) lvalue
t.2) const rvalue , rvalue.
template <class T>
constexpr std::add_const_t<T>& as_const(T& t) noexcept
{
return t;
}
|
__cpp_lib_as_const |
201510L |
(C++17) | std::as_const
|
#include <cassert>
#include <string>
#include <type_traits>
#include <utility>
int main()
{
std::string mutableString = " !";
auto&& constRef = std::as_const(mutableString);
mutableString.clear(); // OK
// constRef.clear(); // : 'constRef' 'const',
// 'clear' 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 " );
static_assert(!std::is_same_v<ExprType, std::string>,
" mutable ." );
}
(C++11) |
, const ( ) |
(C++11)(C++11)(C++11) |
const / volatile ( ) |
(C++11)(C++11)(C++11) |
const / volatile ( ) |
view constant_range ( ) ( ) |