Skip to content

Instantly share code, notes, and snippets.

@stryku
Created July 28, 2017 22:03
Show Gist options
  • Select an option

  • Save stryku/b7a75aa2619961ff36be4fe596398442 to your computer and use it in GitHub Desktop.

Select an option

Save stryku/b7a75aa2619961ff36be4fe596398442 to your computer and use it in GitHub Desktop.
#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