Value Categories and References
Return Value Flow
A returned value can flow directly into a new object without requiring the caller to manage a separate temporary.
Return Value Flow
return_value_flow.cpp
#include <iostream>
#include <string>
std::string make_label(int count) {
return "item" + std::to_string(count);
}
int main() {
int count = ;
std::string label = make_label(count);
int size = static_cast<int>(label.size());
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
std::string make_label(int count) {
return "item" + std::to_string(count);
}
int main() {
int count = ;
std::string label = make_label(count);
int size = static_cast<int>(label.size());
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
#include <iostream>
#include <string>
std::string make_label(int count) {
return "item" + std::to_string(count);
}
int main() {
int count = ;
std::string label = make_label(count);
int size = static_cast<int>(label.size());
std::cout << "label=" << label << std::endl;
std::cout << "size=" << size << std::endl;
return 0;
}
return value
A function can create a value and return it to the caller.
prvalue
A pure rvalue expression can initialize a named object at the call site.