Boost logo

Boost Users :

From: Justin Findlay (findlay_at_[hidden])
Date: 2004-08-30 12:43:29


I've got some code that uses the format library to format output, not out of necessity, but rather out of a wanton enjoyment derived from using the syntax, having lately (relatively) discovered boost, prefering the boost::format simple syntax to the posix printf and std::ostringstream flavored solutions. The "problem" I have is that for the output of every double, I have to specify the precision:

int main()
{
  cout << setprecision(18);
  cout << format("Bunner p' air: %1% torr\t\tN$_2$: %2% torr\n"
                 " C air: %3%\t\tN$_2$: %4%")
                 % group(setprecision(18), bunner::air::pp_av)
                 % group(setprecision(18), bunner::N2::pp_av)
                 % group(setprecision(18), bunner::air::c_av)
                 % group(setprecision(18), bunner::N2::c_av) << endl;
  //...

  return 0;
}

Is there a graceful solution, or am I using this library beyond what it was intended for? Something like the following naturally suggests itself, but I don't think it is standard compliant: the constructor doesn't seem to like using a static object, actually the linker couldn't find the 'prmanip' object.

class Fmt
{
public:
  Fmt()
  {
    cout << "duh" << endl << prmanip << constants::pi << endl;
  }
  static std::_Setprecision setpr(const int&pr)
  {
    prmanip = std::setprecision(pr);
    return std::setprecision(pr);
  }
  static std::_Setprecision prmanip;
};

int main()
{
  cout << setprecision(18);
  Fmt::setpr(18);
  //...

Justin


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