Boost logo

Boost Users :

From: Bo Peng (ben.bob_at_[hidden])
Date: 2004-11-19 13:41:43


Dear list,

In the manual of boost::variant:

OutputStreamable: For any object t of type T, std::cout << t must be a
valid expression.

However, I can not output a variant vector work by defining ostream&
operator<< vector<double> . Attached is a test case. Note that variant
int, string can be outputed, but not the vector. Yet, cout <<
vector<double> is working.

I am using gcc 3.2.2 on linux redhat 9.

Many thanks in advance.
Bo

#include "boost/variant.hpp"
using namespace std;
#include <string>
#include <vector>
typedef boost::variant<int, double, string, vector<double> > Variable;

#include <iostream>

ostream& operator<< (ostream& out, const vector<double> & vec)
{
  out<< "[";
  if( ! vec.empty() )
  {
    vector<double>::const_iterator it = vec.begin();
    out << *it;
    for( ++it; it!= vec.end(); ++it)
      out << ", " << *it ;
  }
  out << "]";
  return out;
}

int
main()
{
  Variable a(2);
  Variable b("asd");
  vector<double> vec(0);
  vec.push_back(3.0);
  Variable c(vec);

  cout << vec << endl; // OK
  cout << a << b << endl; // OK
  cout << c << endl; // compiling errors.
  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net