, std::to_string, const MyEnum ( clang-703.0.31).
#include <iostream>
#include <type_traits>
enum class MyEnum { A, B, C };
template<typename T>
typename std::enable_if<std::is_enum<T>::value, std::ostream &>::type
operator<<(std::ostream& out, const T& t) {
return (out << std::to_string(t));
}
int main() {
MyEnum a;
std::cout << a << std::endl;
}
docs std::enable_if, , valid, T - ( ). . std::is_enum<T>::value true, std::enable_if<std::is_enum<T>::value, std::ostream &>::type std::ostream & ( ) .
- my::to_string, :
namespace my {
template<typename T>
typename std::enable_if<! std::is_void<T>{} && std::is_fundamental<T>{}, std::string>::type
to_string(T t) {
return std::to_string(t);
}
std::string to_string(std::nullptr_t) = delete;
std::string to_string(MyEnum a) {
return "This is an enum";
}
}
my::to_string std::to_string operator<<:
return (out << my::to_string(t));
EDIT: my::to_string , void std::nullptr_t.
docs:
// 2. the second template argument is only valid if T is an integral type:
template < class T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even (T i) {return !bool(i%2);}
, class = /* enable_if stuff */, template< typename T, /* enable_if stuff */ >. , , - , std::to_string, enum .