article thumbnail image
Published 2022. 7. 4. 13:54
  • lvalue  - value category
    • Any object in C++ that is named by the programmer is lvalue.
    • lvalue is intialized with rvalue.
    • the programmer should concern about the life. In other words, the programmer should be careful of the ownership of lvalue.
    • lvalue has its ownership.
  • rvalue - value category 
    • the programmer never have a chance to name rvalue.
    • rvalue is never initialized. It itself is a value.
    • 3, 4.5, "a", true, literals in C++ are ravalue.
  • lvalue reference - type category
    • is a reference, which references to an lvalue object.
    • is an alias to lvalue, it does have the ownership of the object it reference to.
  • rvalue reference - type category
    • is a reference, which references to an rvalue.
    • is a standalone object, it has the sole ownership of the object it reference to.
int a = 5;
/*
	"a" is a named object, so "a" is lvalue.
	"5" is unnamed object, so "5" is rvalue.
	"5" is used to initialize lvalue "a" so, "5" is an rvalue.
*/

int sum(int a, int b){
	int c = a + b;
	return c;
}

int d = sum(5, 6);

/*
	what is the return value type of sum()?
	the "type" of the return value of sum() is "int"
	what is the value category of the function sum()?
	the value category of the function sum() is rvalue!!
*/

int& sum(int& a, int b){
	a+= b;
    return a;
}

/*
	what is the value category of sum(int&, int)
	lvalue reference? No!
    
    the type category of sum() is "int&" or lvalue reference
	the value category of sum() is lvalue.
*/

int sum(int&& a, int& b){ return int{};}
int a = 5;
int b = 9;
int c = sum(a,b);	//it does not compile, because a is int&&\
int c = sum(std::move(a), b)	//this is correct.

int pro(){return 5;}
ind d = sum(pro(),5 )	//will it work?
	// Yes, pro() returns a value of type int, that is not owned by any object.
	// the value category of the return value of pro() is rvalue.
	// the type category of the return value of pro() is int.

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

[C++14] Virtual Inheritance  (0) 2022.09.18
[C++20] Concepts  (0) 2022.09.17
Type Category Testing_2(형 카 검-컴포지트 타입)  (0) 2022.07.04
Template 가이드라인  (0) 2022.07.04
Type Category Testing_1(형 카 검-내장타입)  (0) 2022.07.03
복사했습니다!