Namespaces
Variants

std:: ref, std:: cref

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
헤더 파일에 정의됨 <functional>
template < class T >
std:: reference_wrapper < T > ref ( T & t ) noexcept ;
(1) (C++11부터)
(C++20부터 constexpr)
template < class T >

std:: reference_wrapper < T >

ref ( std:: reference_wrapper < T > t ) noexcept ;
(2) (C++11부터)
(C++20부터 constexpr)
template < class T >
void ref ( const T && ) = delete ;
(3) (C++11부터)
template < class T >
std:: reference_wrapper < const T > cref ( const T & t ) noexcept ;
(4) (C++11부터)
(C++20부터 constexpr)
template < class T >

std:: reference_wrapper < const T >

cref ( std:: reference_wrapper < T > t ) noexcept ;
(5) (C++11부터)
(C++20부터 constexpr)
template < class T >
void cref ( const T && ) = delete ;
(6) (C++11부터)

함수 템플릿 ref cref std::reference_wrapper 타입의 객체를 생성하는 헬퍼 함수로, 템플릿 인자 추론 을 사용하여 결과의 템플릿 인자를 결정합니다.

T 는 불완전한 타입일 수 있습니다.

(C++20부터)

목차

매개변수

t - 래핑할 객체에 대한 lvalue 참조 또는 std::reference_wrapper 인스턴스

반환값

2) t
4) std:: reference_wrapper < const T > ( t )
5) t
3,6) rvalue reference wrapper가 삭제되었습니다.

예제

#include <functional>
#include <iostream>
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // 함수 객체에 저장된 n1의 복사본을 증가시킴
    ++n2; // main()의 n2를 증가시킴
    // ++n3; // 컴파일 오류
}
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}

출력:

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3146 C++11 언래핑 오버로드가 때때로 오류를 발생시킴 항상 유효하도록 수정됨

참고 항목

CopyConstructible CopyAssignable 레퍼런스 래퍼
(클래스 템플릿)