Boost logo

Boost Users :

Subject: [Boost-users] Why is boost::format::operator% not declared const?
From: Chris Stankevitz (chrisstankevitz_at_[hidden])
Date: 2013-01-15 20:08:27


Hello,

I use boost::format to convert a float to a string:

void CalledThousandsOfTimesPerSecond(float f)
{
  GetStream() << boost::format("%0.3f") % f << GetSomeOtherContent();
}

I normally would deal with something like this by adding a static
const variable:

void CalledThousandsOfTimesPerSecondFromManyThreads(float f)
{
  static const boost::format Formatter("%0.3f");

  GetStream() << Formatter % f << GetSomeOtherContent();
}

But this does not work. boost::format::operator% is not declared const.

Question 1: Why is boost::format::operator% declared mutable? What is
is mutating?

Question 2: How do I not spend overhead constructing the same
boost::format thousands of times per second?

Possible answers to question 2:

void CalledThousandsOfTimesPerSecond(float f)
{
  static boost::format Formatter("%0.3f"); // MUTABLE -- this might
not be thread safe???

  GetStream() << Formatter % f << GetSomeOtherContent();
}

void CalledThousandsOfTimesPerSecond(float f)
{
  static const boost::format Formatter1("%0.3f");
  boost::format Formatter2(Formatter1); // lame but I guess it works

  GetStream() << Formatter2 % f << GetSomeOtherContent();
}

Thank you,

Chris


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