首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::as_const

Defined in header <utility>

template <class T> constexpr std::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%29表格引用Const类型t

2%29值引用重载被删除,以不允许rvalue参数。

可能的实施

模板<class T>std::[医]康斯特[医]T型<T>&AS[医]Const%28 t&t%29 no,{ret;}

*。

二次

代码语言:javascript
复制
#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." );
}

二次

另见

is_const (C++11)

checks if a type is const-qualified (class template)

add_cvadd_constadd_volatile (C++11)(C++11)(C++11)

adds const or/and volatile specifiers to the given type (class template)

remove_cvremove_constremove_volatile (C++11)(C++11)(C++11)

removes const or/and volatile specifiers from the given type (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券