Created
July 28, 2017 22:03
-
-
Save stryku/b7a75aa2619961ff36be4fe596398442 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <string> | |
| #include <utility> | |
| #include <type_traits> | |
| auto toString(bool b) | |
| { | |
| return b ? "true" : "false"; | |
| } | |
| template <typename T> | |
| auto toString(T&& val) -> decltype(std::to_string(std::declval<T>())) | |
| { | |
| return std::to_string(val); | |
| } | |
| template <typename T> | |
| auto toString(T&& val) -> decltype(std::string(std::declval<T>())) | |
| { | |
| return val; | |
| } | |
| template <typename T> | |
| auto toString(T&& val) -> decltype(std::declval<T>().toString()) | |
| { | |
| return val.toString(); | |
| } | |
| ////////////////////////////////////////////////// | |
| struct Pos | |
| { | |
| int x, y; | |
| std::string toString() const | |
| { | |
| return "x: " + std::to_string(x) + ", y: " + std::to_string(y); | |
| } | |
| }; | |
| template <typename ...Args> | |
| void print(const Args&... args) | |
| { | |
| (std::cout << ... << (toString(args) + std::string{ "\n" })); | |
| } | |
| int main() | |
| { | |
| int foo{ -20 }; | |
| double bar{ 13.22 }; | |
| bool baz{ false }; | |
| std::string qux{ "qux" }; | |
| Pos alohomora{ 20, 40 }; | |
| print(foo, bar, baz, qux, "Pos: ", alohomora); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment