Obsolete Members for <QtTypeTraits>
<QtTypeTraits> 클래스의 다음 멤버들은 더 이상 사용되지 않습니다. 이 멤버들은 기존 소스 코드가 계속 작동하도록 하기 위해 제공됩니다. 새로운 코드에서는 이 멤버들을 사용하지 않는 것을 강력히 권장합니다.
함수
(deprecated in 6.6) typename std::add_const<T>::type & | qAsConst(T &t) |
(deprecated in 6.6) void | qAsConst(const T &&t) |
함수 문서
[constexpr noexcept, deprecated in 6.6] template <typename T> typename std::add_const<T>::type &qAsConst(T &t)
이 함수는 6.6 버전부터 더 이상 권장되지 않습니다. 새로운 코드에서는 이 함수를 사용하지 않는 것이 좋습니다.
대신 std::as_const()를 사용하십시오.
t 를 const T 로 형변환한 값을 반환합니다.
이 함수는 C++17의 std::as_const()를 Qt에서 구현한 것으로, std::move()와 유사한 형 변환 함수입니다. 하지만 std::move()가 lvalue를 rvalue로 변환하는 반면, 이 함수는 non-const lvalue를 const lvalue로 변환합니다. std::as_const()와 마찬가지로, 이 함수는 rvalue에 대해서는 작동하지 않습니다. rvalue의 경우 매달린 참조를 남기지 않고 효율적으로 구현할 수 없기 때문입니다.
Qt에서 이 함수의 주요 용도는 암시적으로 공유되는 Qt 컨테이너가 분리되는 것을 방지하는 것입니다:
QString s = "...";
for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
process(ch);
for (QChar ch : qAsConst(s)) // ok, no detach attempt
process(ch);물론 이 경우, 애초에 ` s `를 ` const `로 선언할 수도 있었고(그리고 아마도 그렇게 했어야 했을 것입니다):
하지만 종종 그렇게 하기 쉽지 않은 경우가 많습니다.
qAsConst()는 인수를 복사하지 않고 단순히 ` const_cast<const T&>(t)`를 수행한다는 점을 유의해야 합니다. 이것이 바로 rvalue에 대해서는 실패하도록 설계된 이유이기도 합니다. 반환된 참조가 너무 빨리 무효화될 것이기 때문입니다. 따라서 다음 코드는 작동하지만(반환된 객체가 분리됩니다):
for (QChar ch : funcReturningQString())
process(ch); // OK, the returned object is kept alive for the loop's duration다음 코드는 작동하지 않습니다:
for (QChar ch : qAsConst(funcReturningQString()))
process(ch); // ERROR: ch is copied from deleted memory이러한 구문이 컴파일되지 않고(실행 시 오류가 발생하도록) 방지하기 위해, qAsConst()에는 rvalue에 바인딩되는 두 번째(삭제된) 오버로드가 있습니다.
참고: ` QT_NO_QASCONST ` 매크로를 정의하여 `qAsConst()` 함수를 사용할 수 없게 만들 수있습니다 .
[delete, deprecated in 6.6] template <typename T> void qAsConst(const T &&t)
이 함수는 6.6 버전부터 사용 중단되었습니다. 새로운 코드에서는 이 함수를 사용하지 않는 것을 강력히 권장합니다.
다음과 같은 코드에서 매달린 참조가 발생하는 것을 방지하기 위해 이 오버로드는 삭제되었습니다.
for (QChar ch : qAsConst(funcReturningQString()))
process(ch); // ERROR: ch is copied from deleted memory이것은 오버로드된 함수입니다.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.