C++ R-value Reference

2021. 3. 24. 10:35·C++/SmartPointer

R-value Reference

R-value

  • L-value와 달리 메모리 주소가 저장되지 않는 값을 의미한다.

예제

  • 주소를 가지고 있지 않은 리터럴 값이나 함수의 반환 값 등을 참조할 수 있다.

    #include <iostream>
    
    using namespace std;
    
    void        doSomething(int& ref)
    {
      cout << "L-value ref\n";
    }
    
    void        doSomething(int&& ref)
    {
      cout << "R-value ref\n";
    }
    
    int            getResult()
    {
      return 100 * 100;
    }
    
    int            main()
    {
      int x = 5;
      int y = getResult();
      const int cx = 6;
      const int cy = getResult();
    
      // L-value References
    
      int& lr1 = x;
      //int& lr2 = cx;
      //int& lr3 = 5;
    
      const int& lr4 = x;
      const int& lr5 = cx;
      const int& lr6 = 5;
    
    
// R-value references

//int&& rr1 = x;
//int&& rr2 = cx;
int&& rr3 = 5;
int&& rrr = getResult();

cout << rr3 << endl;
rr3 = 10;
cout << rr3 << endl;

//const int&& rr4 = x;
//const int&& rr5 = cx;
const int&& rr6 = 5;

doSomething(x);
doSomething(5);
doSomething(getResult());

}

/* stdout stderr
5
10
L-value ref
R-value ref
R-value ref
*/
```

저작자표시

'C++ > SmartPointer' 카테고리의 다른 글

C++ 순환 의존성 문제 (Circular Dependency Issues)  (0) 2021.03.24
C++ 이동 생성자와 이동 대입 (Move Constructor and Move Assignment)  (0) 2021.03.24
C++ Syntax vs Semantics  (0) 2021.03.24
C++ 스마트 포인터 (Smart Pointer)  (0) 2021.03.22
'C++/SmartPointer' 카테고리의 다른 글
  • C++ 순환 의존성 문제 (Circular Dependency Issues)
  • C++ 이동 생성자와 이동 대입 (Move Constructor and Move Assignment)
  • C++ Syntax vs Semantics
  • C++ 스마트 포인터 (Smart Pointer)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (316)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (19)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    dism
    vscode
    스프링
    spring
    백기선
    그림판
    EXCLUDE
    MacOS
    SunOS 5.1
    windows
    윈도우
    스프링 프레임워크 핵심 기술
    java
    맥북 카카오톡 알림 안뜸
    알림
    시스템 복구
    Windows 11
    logi options
    citrix workspace
    KakaoTalk
    mspaint
    로지텍 마우스 제스처
    unix
    Solaris 10
    윈도우 명령어
    SFC
    제외
    Workspace
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ R-value Reference
상단으로

티스토리툴바