Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:23:10+00:00 2026-05-26T18:23:10+00:00

I was wondering whether Boost.Format does support using a fixed-width / preallocated buffer as

  • 0

I was wondering whether Boost.Format does support using a fixed-width / preallocated buffer as output instead of a dynamic buffer managed by the lib itself?

That is, normally you’d do:

boost::format myfmt("arg1: %1% / arg2: %2%");
// e.g.:
cout << (myfmt % 3.14 % 42);
// or
string s = boost::str( myfmt % "hey!" % "there!");

so the Boost:Format lib will automatically take care of allocating enough space and managing the “output buffer” for you.

I was wondering if there’s any way to use a predefine non-dynamic buffer with Boost.Format, that is, something like:

const size_t buf_sz = 512;
char big_enough[buf_sz];
boost::format myfmt("arg1: %1% / arg2: %2%");
myfmt.attach_buffer(big_enough, buf_sz);
myfmt % "hey!" % "there!"
// big_enough buffer now contains the result string

I know I could just sift through the examples, the docs and the source, but apart from lacking time atm. (and the very possibility of missing something) it would be interesting to know:
If it is not possible, it would be great if someone could explain why (if there is/are specific whys) — was it deliberate? doesn’t it match the API well? …?

Disclaimer: This question is not about performance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:23:10+00:00Added an answer on May 26, 2026 at 6:23 pm

    Initial Idea

    Looking at the source it seems you can use your own allocator which is then used by the internal stream (internal_streambuf_t) of boost::format. Would that be good enough for your case?

    For example you could use something like the libstdc++ array_allocator

    Unfortunately boost::format also uses a couple of std::vector which do not use the custom allocator which may be a problem in your case?

    How boost::format works

    I looked into the source of boost::format and this is how it works (described below is str(), << calls either str() or uses standard std::ostream stuff) :

    • the format class stores all arguments and format string separatly, sometimes using the custom allocator, sometimes using the default allocator
    • when str() is called it creates a new std::string and makes it large enough for the result using the custom allocator
    • it then appends all arguments and static string pieces from the format string to the result string
    • finally it returns the result string by value

    So, the final result string is not stored inside the format class but created when needed.

    So even if you can find the location of the result string when using a custom allocator, it is only available after/during a call to str().
    This should explain why it is not possible: the formatted result is never stored inside an “output buffer” in the class.

    Why it works like this

    Why they did it this way I do not know. I think it is because you can only build the result after all arguments are known, it wastes space to store the result and you probably only need the result just once for a given format/argument combination. So creating it when needed does not result in extra work since typically str() is only called once.

    Solutions

    • Create some wrapper around str() or << and copy the result into your fixed buffer
    • Use a stream_buffer to ‘stream’ the string into the buffer (see example below)
    • Inherit the class and add your own str() function which stores the result in a fixed buffer.

    Possible solution using boost::iostreams (tested):

    #include <iostream>
    #include <boost/format.hpp>
    #include <boost/iostreams/stream.hpp>
    
    int main()
    {
        char buffer[100];
    
        boost::iostreams::stream<boost::iostreams::array_sink>
            stream(buffer, sizeof(buffer));
    
        stream << (boost::format("arg1 = %1%") % 12.5);
        stream << '\0';  // make sure buffer contains 0-terminated string
    
        std::cout << buffer << std::endl;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering whether using a Belt and Braces (Suspenders) approach to programming -
I've been wondering whether using prototypes in JavaScript should be more memory efficient than
I was wondering whether there's any advantages to using a static member function when
I was wondering whether there is a way in Boost.Spirit.Qi to dynamically combine an
I am wondering whether what I want can be acheived by using Yahoo pipes
Just wondering whether anyone knows how to get blogger labels into alt tags in
I'm wondering whether anyone here has ever used a skip list . It looks
I've been wondering whether there is a good "git export" solution that creates a
I'm wondering whether something like this is possible (and relatively easy to do), and
I was wondering whether overriding conversion operators is only applicable to numeric types? If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.