我刚刚发现了如何检查是否operator<<
为类型提供了。
template<class T> T& lvalue_of_type();
template<class T> T rvalue_of_type();
template<class T>
struct is_printable
{
template<class U> static char test(char(*)[sizeof(
lvalue_of_type<std::ostream>() << rvalue_of_type<U>()
)]);
template<class U> static long test(...);
enum { value = 1 == sizeof test<T>(0) };
typedef boost::integral_constant<bool, value> type;
};
这个技巧是众所周知的,还是我刚刚获得了元编程诺贝尔奖?;)
编辑:我通过两个全局函数模板声明lvalue_of_type
和rvalue_of_type
.