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

  • SEARCH
  • Home
  • 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 7807601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:54:06+00:00 2026-06-02T02:54:06+00:00

I’m writing an alternative to sprintf() using recursive variadic templates, as explained in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf

  • 0

I’m writing an alternative to sprintf() using recursive variadic templates, as explained in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf. My goal is to allow easy addition of custom data type formatters for user-defined types. For example, if the basic implementation looks like this:

#include <iostream>
#include <sstream>
#include <wchar.h>
#include <stdexcept>

using std::wstring;
using std::wstringstream;

const wstring wsprintf(const wchar_t *s)
{
    wstringstream outstream;
    while(*s)
    {
        if (*s == L'%' && *++s != L'%')
            throw std::runtime_error("invalid format string: missing arguments");
        outstream << *s++;
    }
    return outstream.str();
}

template<typename T, typename... Args>
const wstring wsprintf(const wchar_t *s, const T& value, const Args&... args)
{
    wstringstream outstream;
    while(*s)
    {
        if(*s == L'%' && *++s != L'%')
        {
            outstream << value << wsprintf(++s, args...);
            return outstream.str();
        }
        outstream << *s++;
    }
    throw std::runtime_error("extra arguments provided to wsprintf");
}

then I could add a formatter for my class Foo (which, let’s say, contains the method customDescription() that returns a wstring) by writing

template<typename... Args>
const wstring wsprintf<const Foo&>(const wchar_t *s, const Foo& foo, const Args&... args)
{
    return wsprintf(s, foo.customDescription(), args...);
}

I would then be able to do this:

Foo bar;
wstring message = wsprintf("my foo tells me %s", bar);

However, the way I’ve written this code will not work because partial template specialization for functions (PTSF) is not allowed, as explained in http://www.gotw.ca/publications/mill17.htm.

The two alternatives generally available in lieu of PTSF are:

  1. Eliminate the use of templates altogether and use overloaded functions.
  2. Create static classes to wrap specialized implementations of the function.

The first alternative doesn’t seem feasible because the recursive variadic template approach to printf() requires at least one template argument (the variadic parameter pack).

When I tried to implement the second alternative, I ran into several syntax errors (inline as comments):

namespace wsprintf_impl {

    struct wsprintf
    {
        static const wstring impl(const wchar_t *s)
        {
            wstringstream outstream;
            while(*s)
            {
                if (*s == L'%' && *++s != L'%')
                    throw std::runtime_error("invalid format string: missing arguments");
                outstream << *s++;
            }
            return outstream.str();
        }
    };

    // ERROR: redefinition of 'wsprintf' as different kind of symbol
    template< class T, class Args&... args >
    struct wsprintf
    {
        static const wstring impl(const wchar_t *s, const T& value, const Args&... args)
        {
            wstringstream outstream;
            while(*s)
            {
                if(*s == L'%' && *++s != L'%')
                {
                    outstream << value << wsprintf::impl(++s, args...);
                    return outstream.str();
                }
                outstream << *s++;
            }
            throw std::runtime_error("extra arguments provided to wsprintf");
        }
    };

}

template< class T, class Args&... args >
wstring wsprintf(const wchar_t *s, const T& value, const Args&... args)
// ERROR: type 'const Args &' of function parameter pack does not contain any unexpanded parameter packs
// ERROR: declaration of 'args' shadows template parameter
{
    return wsprintf_impl::wsprintf<T, args...>::impl(s, value, args...);
    // ERROR: expected '>'
    // ERROR: expected '(' for function-style cast or type construction
}

I’m not sure how to fix these errors. Any ideas? Am I on the right path in the first place?

  • 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-06-02T02:54:07+00:00Added an answer on June 2, 2026 at 2:54 am

    The problem is that wsprintf is declared as both a class and a class template. Just make it be a class template, and the first case is the specialization for no arguments:

    template <typename...>
    struct wsprintf;
    
    template <>
    struct wsprintf<> // specialization for no arguments
    {
        // blah blah ... 
    };
    
    template< class T, class... Args> // oops, bad syntax here was
    struct wsprintf<T, Args...> // specialization for one or more args.
    {
        // blah blah ... 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

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.