Which one is better: boost::format or sprintf?
I also want to know which one is more type safe to use.
I know one limitation with sprintf that it formats a char[] object, and the size of char[] should be sufficient to hold the formatted string. But does boost::format also hold the same limitation or not. can Anyone point out me a doc on net which I would refer for the difference among boost::format and sprintf.
In terms of robustness and type-safety boost::format is a clear winner. It doesn’t have an issue with buffer size as it uses a dynamic stream. The types of the parameters are retained in the function call, thus the formatter can intelligently convert. This also means there is no problem of screwing up the stack with mismatched types. Type conversion is also available in this model.
The drawbacks of format are its speed and that it is a bit cumbersome. Its speed is quite a bit lower than that of sprintf for formats that sprintf can handle. Its syntax is also a bit more involved than a quick call to sprintf.
For simple token formatting I usually use sprintf. For complex string formatting I tend to use boost format, or iostreams.