Monday 15 March 2010

c++ - check for whether a type instance can be streamed -



c++ - check for whether a type instance can be streamed -

i've puzzled on meta-function long time. seems work, suspect contain ub, when checks size of perchance undefined reference type? there problem meta-function?

template <class s, class c, typename = void> struct is_streamable : ::std::false_type { }; template <class s, class c> struct is_streamable<s, c, decltype(void(sizeof(decltype(::std::declval<s&>() << ::std::declval<c const&>())))) > : ::std::true_type { };

edit: motivation question (and worries) question. why did not utilize similar trick (checking size of reference type)?

when checks size of perchance undefined reference type?

that never induces ub. result in deduction failure (according [temp.deduct]/8), causing primary template instantiated. [expr.sizeof]:

when applied reference or reference type, result size of referenced type.

the sizeof operator shall not applied look has […] incomplete type […].

but yet incomplete ostream regarded "streamable" string if global operator<< overloaded. prepare that, define partial specialization as

template <class s, class c> struct is_streamable<s, c, decltype(void( std::declval<s&>() << std::declval<c const&>() )) > : ::std::true_type {};

demo.

c++ c++11 iostream template-meta-programming sfinae

No comments:

Post a Comment