Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- #include <type_traits>
- #include <iostream>
- #include <string>
- template <size_t N>
- struct priority_tag : priority_tag<N-1> {};
- template <>
- struct priority_tag<0> {};
- template <typename T, typename = decltype(std::declval<T>().toString())>
- void print(T&& value, const priority_tag<2> &) {
- std::cout << value.toString() << "\n";
- }
- template <typename T>
- void print(T&& value, const priority_tag<1> &) {
- std::cout << value << "\n";
- }
- template <typename T>
- void print(T&& value) {
- print(std::forward<T>(value), priority_tag<2>{});
- }
- struct Foo {
- std::string toString() { return "Foo"; }
- };
- struct Bar {
- /* Doesn't have tOString, but does have an operator<< we can use. */
- friend std::ostream &operator<<(std::ostream &os, const Bar &b) {
- os << "Bar";
- return os;
- }
- };
- int main() {
- Foo f;
- Bar b;
- print(f);
- print(b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement