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 6681627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:36:41+00:00 2026-05-26T04:36:41+00:00

I’m using Visual Studio 2008, and I want to implement string formatting function without

  • 0

I’m using Visual Studio 2008, and I want to implement string formatting function without Variable Argument List.

How to implement “Variadic Template” with pre-c++0x(VS2008)?

Is there any library which implements this like boost?

Or another way to implement this?

Here is my sample code.
(of course, this can’t be complied because i’m using VS2008.)

bool VarPrint(std::ostringstream& out, const std::string& s) 
{
    std::string::size_type offset = 0;
    if((offset = s.find("%")) != std::string::npos)
    {
        if(!(offset != s.size() - 1 && s[offset + 1] == '%'))
        {
            ASSERT(!"Missing Arguments!");
            return false;
        }
    }
    out << s;
    return true;
}

template<typename T, typename... Args>
bool VarPrint(std::ostringstream& out, const std::string& s, const T& value, const Args&... args) 
{
    std::string::size_type prev_offset = 0;
    std::string::size_type curr_offset = 0;
    while((curr_offset = s.find("%", prev_offset)) != std::string::npos)
    {
        out << s.substr(prev_offset, curr_offset);
            if(!(curr_offset != s.size() - 1 && s[curr_offset + 1] == '%'))
        {
            out << value;
            if(curr_offset + 2 < s.length())
                return VarPrint(out, s.substr(curr_offset + 2), args...);                   return true;
        }

        prev_offset = curr_offset + 2;
        if(prev_offset >= s.length)
            break;
    }
    ASSERT(!"Extra Argument Provided!");
    return false;
}
  • 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-26T04:36:42+00:00Added an answer on May 26, 2026 at 4:36 am

    In C++03, you have different possibilities:

    1. generate overloads for 0-N arguments (using Boost.Preprocessor for example)
    2. use Cons-Lists (cons(1)("some string")(foo))
    3. use object and overload some operator (operator() for example, or operator% like Boost.Format)

    The first option is a bit tricky, I feel, because not everyone can understand macros easily, so I would only reserve it for short-terms solutions if you plan on migrating to C++0x soon.

    The third option may provide a nice custom touch (formatting is done with a % sign in many languages), but it also means that one needs to remember how this particular “variadic” function works each time.

    My personal preference is the cons approach because it solves both issues:

    • the definition involves only templates, so it is more readable and maintanable than 1.
    • you define the cons-machinery once, and you can then re-use it for any “variadic” function (and they remain functions), so it is more consistent, and saves you work

    For example, here is how it could work:

    The includes that this example will use:

    #include <cassert>
    #include <iostream>
    #include <string>
    

    A helper for the result type of appending a value (it could be more efficient with prepending, but that would mean passing the arguments in reverse order which is counter-intuitive):

    template <typename T, typename Next> struct Cons;
    struct ConsEmpty;
    
    template <typename Cons, typename U>
    struct cons_result;
    
    template <typename U>
    struct cons_result<ConsEmpty, U> {
      typedef Cons<U, ConsEmpty> type;
    };
    
    template <typename T, typename U>
    struct cons_result<Cons<T, ConsEmpty>, U> {
      typedef Cons<T, Cons<U, ConsEmpty> > type;
    };
    
    template <typename T, typename Next, typename U>
    struct cons_result<Cons<T, Next>, U> {
      typedef Cons<T, typename cons_result<Next, U>::type> type;
    };
    

    The Cons template itself, with a magic operator() to append value. Note that it creates a new item with a different type:

    template <typename T, typename Next>
    struct Cons {
      Cons(T t, Next n): value(t), next(n) {}
    
      T value;
      Next next;
    
      template <typename U>
      typename cons_result<Cons, U>::type operator()(U u) {
        typedef typename cons_result<Cons, U>::type Result;
        return Result(value, next(u));
      }
    };
    
    struct ConsEmpty {
      template <typename U>
      Cons<U, ConsEmpty> operator()(U u) {
        return Cons<U, ConsEmpty>(u, ConsEmpty());
      }
    };
    
    template <typename T>
    Cons<T, ConsEmpty> cons(T t) {
      return Cons<T, ConsEmpty>(t, ConsEmpty());
    }
    

    A revisited VarPrint with it:

    bool VarPrint(std::ostream& out, const std::string& s, ConsEmpty) {
        std::string::size_type offset = 0;
        if((offset = s.find("%")) != std::string::npos) {
            if(offset == s.size() - 1 || s[offset + 1] != '%')  {
                assert(0 && "Missing Arguments!");
                return false;
            }
        }
        out << s;
        return true;
    }
    
    template<typename T, typename Next>
    bool VarPrint(std::ostream& out,
                  std::string const& s,
                  Cons<T, Next> const& cons) 
    {
        std::string::size_type prev_offset = 0, curr_offset = 0;
        while((curr_offset = s.find("%", prev_offset)) != std::string::npos) {
            out << s.substr(prev_offset, curr_offset);
            if(curr_offset == s.size() - 1 || s[curr_offset + 1] != '%') {
                out << cons.value;
                if(curr_offset + 2 < s.length())
                    return VarPrint(out, s.substr(curr_offset + 2), cons.next);
                return true;
            }
            prev_offset = curr_offset + 2;
            if(prev_offset >= s.length())
                break;
        }
        assert(0 && "Extra Argument Provided!");
        return false;
    }
    

    And the demo:

    int main() {
      VarPrint(std::cout, "integer %i\n", cons(1));
      VarPrint(std::cout, "mix of %i and %s\n", cons(2)("foo"));
    }
    

    Output:

    integer 1
    mix of 2 and foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker

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.