template< class T >
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.
[edit] Possible implementationtemplate<class T> constexpr std::add_const_t<T>& as_const(T& t) noexcept { return t; }[edit] Notes [edit] Example
#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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4