
Hi, I am wondering how to create a 'type trait' such as 'is_ostreamable', ie: class A {}; class B {}; std::ostream &operator<<(std::ostream &os, const A &in) { return (os << in); } // Note, no operator<< for class B // --- somewhere else .. ;) class X { std::ostream out; template<typename T> void Impl(const T &in, boost::mpl::bool_<true>) { out << "(" << typeid(in) << ") " << in; } template<typename T> void Impl(const T &in, boost::mpl::bool_<false) { out << "(" << typeid(in) << ") 0x" << std::hex << (unsigned int) ∈ } public: X(std::ostream &os) : out(os) {} template<typename T> void DoIt(const T &in) { Impl(in, boost::mpl::bool_<boost::is_ostreamable<T> >()); } }; So the above would print the type name, and then the value of the entry IF the entry could be streamed to a std::ostream. Otherwise, it would print the type name and the address of its memory location (in hex). Obviously the above is just an example, but one that illustrates my need :) Question is, how to do it? Its easy enough to get a compiler error when something is not supported, but how to get the compiler to choose behavior on whether something is supported. -- PreZ :) Founder. The Neuromancy Society (http://www.neuromancy.net)

On Sat, 16 Apr 2005 01:45:11 -0400, Preston A. Elder wrote:
I am wondering how to create a 'type trait' such as 'is_ostreamable', ie: --> SNIP <--
Just as an FYI, here is my current attempts to get it working. With the code as follows, both A and B show up as 'not convertable' (aka. not streamable). As mentioned in the comments, if I uncomment the templated constructor of streamable_object, both show up as 'convertable'. Here is the code: #include <cstdio> #include <sstream> #include <boost/type_traits.hpp> class A { int val; public: A(int v) : val(v) {} int get() const { return val; } }; std::ostream &operator<<(std::ostream &out, const A &in) { return (out << in.get()); } class B { int val; public: B(int v) : val(v) {} int get() const { return val; } }; // ---------------------------------------------- template<typename T> struct streamable_object { streamable_object() {} // If this is uncommented, is_convertable will return true no matter // what class T is. But is_convertable states it doesn't work with // constructor-based conversions anyway, so not surprising. //streamable_object(const T &in) //{ // std::stringstream os; // os << in; //} streamable_object &operator=(const T &in) { std::stringstream os; os << in; } }; int main() { printf("RES(A) = %d\n", boost::is_convertible<A, streamable_object<A> >::value); printf("RES(B) = %d\n", boost::is_convertible<B, streamable_object<B> >::value); A a(5); B b(10); std::stringstream ss; ss << a; // ERROR: //ss << " " << b; printf("RES(SS) = %s\n", ss.str().c_str()); streamable_object<A> sa; sa = a; // ERROR: //streamable_object<B> sb(b); //sb = b; return 0; } -- PreZ :) Founder. The Neuromancy Society (http://www.neuromancy.net)

On Sat, 16 Apr 2005 01:45:11 -0400, Preston A. Elder wrote:
I am wondering how to create a 'type trait' such as 'is_ostreamable', ie: --> SNIP <--
Google/the boost list is my savior :) There is a solution here: http://lists.boost.org/MailArchives/boost/msg71111.php And it WORKS! :) The question is now, what is the status of this being included into boost? This is a very highly useful library :) -- PreZ :) Founder. The Neuromancy Society (http://www.neuromancy.net)

Hello Preston, Preston A. Elder wrote:
On Sat, 16 Apr 2005 01:45:11 -0400, Preston A. Elder wrote:
There is a solution here: http://lists.boost.org/MailArchives/boost/msg71111.php
And it WORKS! :)
The question is now, what is the status of this being included into boost? This is a very highly useful library :)
It's under development. Unfortunately the process is rather slow lately, because Terje and me are very busy. There's still a lot to be done to consistently meet Boost quality and there are some very useful changes in the pipeline [ inspired by this paper: http://tinyurl.com/arqo9 ], however, these are only my sketches, right now. It is currently in flux and it will be another few months until we can talk about an "official preview version". The part of the interface which deals with operator detection is pretty much stable (although there will be minor changes - e.g. I'm planning on renaming 'has_comma_op' to 'has_user_defined_comma_op', otherwise it would always have to return 'true' to be consistent with the others; probably the ones that cannot be detected without registration will be moved away from this directory). Regards, Tobias

It's under development. Unfortunately the process is rather slow lately, because Terje and me are very busy. Well, just FYI, I've taken the zip file that was originally posted, and
On Sat, 16 Apr 2005 12:28:09 +0200, Tobias Schwinger wrote: pulled it verbatim into my open source project (keeping the exact source tree in tact, just putting it under an 'ext' directory which is where I put all external code (often that enhances boost)). I've got my configure script setup so that once it detects its been merged into boost, it will use the boost version (by detecting the boost/operator_traits/operator_traits.hpp header). Thanks for writing this - Right now speed is not the primary issue, since I'm using this as part of some trace code that in most production environments, will never be called. -- PreZ :) Founder. The Neuromancy Society (http://www.neuromancy.net)
participants (2)
-
Preston A. Elder
-
Tobias Schwinger