Boost logo

Boost Users :

From: Andy Stevenson (andy.stevenson_at_[hidden])
Date: 2008-04-06 17:15:10


Hi,
 
Sorry trivial questions I'm sure.
Why doesn't the lexical_cast work below?
I think it meets the concept description.

* Source is OutputStreamable, meaning that an operator<< is defined
that takes a std::ostream or std::wostream object on the left hand side and
an instance of the argument type on the right.

* Target is InputStreamable, meaning that an operator>> is defined
that takes a std::istream or std::wistream object on the left hand side and
an instance of the result type on the right.

* Target is CopyConstructible [20.1.3].

* Target is DefaultConstructible, meaning that it is possible to
default-initialize an object of that type [8.5, 20.1.4].

>>>>> play.cpp >>>>
 
#include <vector>
#include <iostream>
#include <algorithm>
 
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <sstream>
 
using namespace std;
using namespace boost;
using namespace boost::filesystem;
 
template<class T>
ostream&
operator<< (ostream& os, const vector<T>& v)
{
  copy(v.begin(), v.end(), ostream_iterator<T>(os, ";"));
  return os;
}
 
template<class T>
string
v2s(const vector<T>& v)
{
  stringstream ss;
  copy(v.begin(), v.end(), ostream_iterator<T>(ss, ";"));
  return ss.str();
}
 
int
main(int argc,
     char* argv[])
{
  vector<string> v;
  v.push_back("hello");
  v.push_back("world");
  
  cout << v << endl;
  
  cout << v2s(v) << endl;
  
  cout << lexical_cast<string>(v) << endl; // XXX doesn't compile...XXX
  return 0;
}

Any help greatly appreciated.
 
Andy
 



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