operator<< for boost::any

Dear all, I am trying to use the any class in a project. I would need to print elements of a vector<any>. I have come to the following solution as an example: template<class T> inline ostream& operator<<(ostream &os, const vector<T> &v) { vector<T>::const_iterator iter(v.begin()); for (int c(0), n(v.size()); c<n-1; c++) os << *iter++ << ','; return os << *iter; } typedef vector<bool> vectb; int main() { vector<any> values; double f(2.0); string s("hello"); int i(100); bool b(false); vectb vectbool; vectbool.push_back(true); vectbool.push_back(false); vectbool.push_back(true); vectbool.push_back(true); vectbool.push_back(false); vectbool.push_back(true); values.push_back(f); values.push_back(s); values.push_back(i); values.push_back(b); values.push_back(vectbool); for (vector<any>::iterator i(values.begin()), last(values.end()); i!=last; i++) { if (any_cast<bool>(i)) cout << *any_cast<bool>(i) << endl; else if (any_cast<int>(i)) cout << *any_cast<int>(i) << endl; else if (any_cast<float>(i)) cout << *any_cast<float>(i) << endl; else if (any_cast<double>(i)) cout << *any_cast<double>(i) << endl; else if (any_cast<string>(i)) cout << *any_cast<string>(i) << endl; else if (any_cast<vectb>(i)) cout << *any_cast<vectb>(i) << endl; } } But I would like a more general solution without to make assumptions about the type. Do you have any ideas? Another point is that I cannot figure out how to convert the solution below in terms of a friend ostream& operator<<(ostream &os, const any &rhs) I cannot get this compiled with g++ v 2.95 friend ostream& operator<<(ostream &os, const any &rhs) { return os << *any_cast<bool>(rhs); } Sincerely, Patrick ====================================================================== Patrick Guio Institute of Physics, University of Oslo P.O. box 1048, Blindern, N-0316 Oslo Tel : (+47) 22 84 40 60 - Fax : (+47) 22 85 56 71 E-mail : patrick.guio@fys.uio.no URL : http://folk.uio.no/~patricg

This question recently got added to the user FAQ on the Wiki. Here's my interpretation, but I thought I would boost it here so Jeremy or others might comment.... http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?BoostUserFAQ Q How should I interpret the Boost Graph Library license? As I read it, it doesn't even look like I'm allowed to use it. Since I am looking for a graph library for use in a (semi) commercial project, this is quite important. A. There should not be a problem. Based on the 1_25_0 license it looks like: 'You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution ' Jeff

On Sat, 1 Dec 2001, Jeff Garland wrote: jeff> jeff> This question recently got added to the user FAQ on the Wiki. Here's my jeff> interpretation, but I thought I would boost it here so Jeremy or others might jeff> comment.... jeff> jeff> http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?BoostUserFAQ jeff> jeff> Q How should I interpret the Boost Graph Library license? As I jeff> read it, it doesn't even look like I'm allowed to use it. jeff> Since I am looking for a graph library for use in a (semi) jeff> commercial project, this is quite important. jeff> jeff> A. There should not be a problem. Based on the 1_25_0 license jeff> it looks like: 'You may not charge a fee for this Package jeff> itself. However, you may distribute this Package in aggregate jeff> with other (possibly commercial) programs as part of a larger jeff> (possibly commercial) software distribution ' That is correct. You are certainly allowed to use the BGL in a commercial product and redistribute it. You just can't charge a fee over and above a reasonable copying fee for the BGL itself. But you can, of course, charge whatever you want for your own software built on top of the BGL. Cheers, Jeremy ---------------------------------------------------------------------- Jeremy Siek http://php.indiana.edu/~jsiek/ Ph.D. Student, Indiana Univ. B'ton email: jsiek@osl.iu.edu C++ Booster (http://www.boost.org) office phone: (812) 855-3608 ----------------------------------------------------------------------

On 12/1/01 4:09 AM, "Patrick Guio" <patrick.guio@fys.uio.no> wrote:
I cannot get this compiled with g++ v 2.95 friend ostream& operator<<(ostream &os, const any &rhs) { return os << *any_cast<bool>(rhs); }
What's the intent of using the word "friend" here? I think that might be the problem. You can declare this function without "friend". -- Darin

On Sat, 1 Dec 2001, Darin Adler wrote:
On 12/1/01 4:09 AM, "Patrick Guio" <patrick.guio@fys.uio.no> wrote:
I cannot get this compiled with g++ v 2.95 friend ostream& operator<<(ostream &os, const any &rhs) { return os << *any_cast<bool>(rhs); }
What's the intent of using the word "friend" here? I think that might be the problem. You can declare this function without "friend".
I have tried without the friend declaration but it does not help.... Anyway, can you think of any general solution, I mean without specifying a list of ValueType ? Sincerely, Patrick
participants (4)
-
Darin Adler
-
Jeff Garland
-
Jeremy Siek
-
Patrick Guio