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;
};
このトリックはよく知られていますか、それともメタプログラミングのノーベル賞を受賞したばかりですか?;)
編集:コードを理解しやすくし、2つのグローバル関数テンプレート宣言lvalue_of_type
とで適応しやすくしましrvalue_of_type
た。