Created
October 14, 2017 11:33
-
-
Save shintakezou/3793ada49fdbfa8be86ad8d7ca613198 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
std::string gimme_a_str(const std::string& s) | |
{ | |
return "string(\"" + s + "\")"; | |
} | |
template<typename T> | |
void action_a(T& a) | |
{ | |
std::cout << "action_a&/*" << a << "*/\n"; | |
} | |
template<typename T> | |
void action_a(T&& a) | |
{ | |
std::cout << "action_a&&/*" << a << "*/\n"; | |
} | |
template<typename T> | |
void action_b(T& a) | |
{ | |
std::cout << "begin&\n\t"; | |
action_a(a); | |
std::cout << "end&;\n"; | |
} | |
template<typename T> | |
void action_b(T&& a) | |
{ | |
std::cout << "begin&&\n\t"; | |
action_a(a); | |
std::cout << "end&&;\n"; | |
} | |
template<typename T> | |
void action_f(T& a) | |
{ | |
std::cout << "forward&\n\t"; | |
action_a(std::forward<T>(a)); | |
std::cout << "drawrof&;\n"; | |
} | |
template<typename T> | |
void action_f(T&& a) | |
{ | |
std::cout << "forward&&\n\t"; | |
action_a(std::forward<T>(a)); | |
std::cout << "drawrof&&;\n"; | |
} | |
int main() | |
{ | |
std::string lv{"L1"}; | |
auto ttl = [](const std::string& s) { | |
std::cout << std::setfill('=') | |
<< std::setw(70) << (" " + s + "\n"); | |
}; // stupid pointless "modern C++" exhibitionism | |
ttl("lvalue call"); | |
action_a(lv); | |
ttl("rvalue call"); | |
action_a(gimme_a_str("0")); | |
ttl("lvalue (no forwarding)"); | |
action_b(lv); | |
ttl("lvalue (forwarding)"); | |
action_f(lv); | |
ttl("rvalue (no forwarding)"); | |
action_b(gimme_a_str("1")); | |
ttl("rvalue (forwarding)"); | |
action_f(gimme_a_str("2")); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment