Skip to content

Instantly share code, notes, and snippets.

@srz-zumix
Last active December 1, 2020 00:29
Show Gist options
  • Select an option

  • Save srz-zumix/e70b72d1c9649e3c1fe918f8d4521949 to your computer and use it in GitHub Desktop.

Select an option

Save srz-zumix/e70b72d1c9649e3c1fe918f8d4521949 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <type_traits>
#include "donot-change.h"
struct X
{
};
template <class T>
concept printable = requires (T x) { ::std::cout << x; };
template<typename T>
requires (!printable<T>)
void print(const T&)
{
::std::cout << "unknown" << ::std::endl;
}
int main()
{
print(1);
print(nullptr);
print(X());
}
// https://wandbox.org/permlink/yznTfVoLiDd3deC9
template<typename T>
void print(const T& val)
{
::std::cout << val << ::std::endl;
}
#include <iostream>
#include <cstdlib>
#include <type_traits>
#include "donot-change.h"
struct X
{
};
void print(...)
{
::std::cout << "unknown" << ::std::endl;
}
template<typename T>
void print(const T& val, typename std::enable_if<std::is_same<decltype((::std::cout << std::declval<T>()), int()), int>::value>::type* = nullptr)
{
::std::cout << val << ::std::endl;
}
#if __GNUC__ < 8
template<>
void print(const ::std::nullptr_t&)
{
::std::cout << "nullptr" << ::std::endl;
}
#endif
int main()
{
print(1);
print(nullptr);
print(X());
}
// https://wandbox.org/permlink/5Yoc4b6Px7V89r17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment