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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:40:38+00:00 2026-05-19T14:40:38+00:00

I am trying to expand my template programming skills and I am facing a

  • 0

I am trying to expand my template programming skills and I am facing a problem to which I don’t see the right solution for.
This is a personal training execise only to do some more advanced templating.

This the goal : write a template to convert any integer type (using sprintf or swprintf) to either string or wstring depending on the type of the format sring.
There is no need for error-checking (for now anuway).

The problem is when an format is specified as (const char*) NULL or (const wchar_t*) NULL

I need to supply a default LITERAL value as either "%i" or L"%i"
an for that I need to determine the char-type of the format-variable.
I am using a functions for that now ,using SFINAE.
However I would like to use a variable for that ,but I don’t think SFINAY works on varaiables (or am i wrong).

Here is my (working) code so far:

////////////////////////////////////////////////////////////////////////////////

template < typename T ,typename I > 
inline 
typename std::enable_if< std::is_same< T ,char >::value ,int >::type 
str_printf ( T* szBuff ,int iLen ,const T* szFrmt ,I iNum )
{ return sprintf_s( szBuff ,iLen ,szFrmt ,iNum ); }

template < typename T ,typename I > 
inline 
typename std::enable_if< std::is_same< T ,wchar_t >::value ,int >::type 
str_printf ( T* szBuff ,int iLen ,const T* szFrmt ,I iNum )
{ return swprintf_s( szBuff ,iLen ,szFrmt ,iNum ); }

////////////////////////////////////////////////////////////////////////////////

template < typename T > 
inline 
typename std::enable_if< std::is_same< T ,char >::value ,const char* >::type 
Dflt_Frmt ()    { return "%i"; }

template < typename T > 
inline 
typename std::enable_if< std::is_same< T ,wchar_t >::value ,const wchar_t* >::type 
Dflt_Frmt ()    { return L"%i"; }

////////////////////////////////////////////////////////////////////////////////

template < typename T ,typename I > 
inline 
std::basic_string< T ,std::char_traits < T > > 
to_string ( I iNum ,const T* pszFrmt )
{
    const int iLen (65);
    T szBuff [iLen] = {0};

    std::basic_string< T ,std::char_traits < T > > frmt ((pszFrmt && (*pszFrmt)) ? pszFrmt : Dflt_Frmt<T>() );
    str_printf( szBuff ,iLen ,frmt.c_str() ,iNum );

    return szBuff;
}

////////////////////////////////////////////////////////////////////////////////

this this what i would like to do (obviously it’s not workin)

////////////////////////////////////////////////////////////////////////////////

template < typename T ,typename I > 
inline 
std::basic_string< T ,std::char_traits < T > > 
to_string ( I iNum ,const T* pszFrmt )
{
    const int iLen (65);
    T szBuff [iLen] = {0};

    // declare a Variable of const T* and initialie it with "%i" or L"%i"
    typename std::enable_if< std::is_same< T ,char >::value      ,const char* >::type        dft("%i");
    typename std::enable_if< std::is_same< T ,wchar_t >::value   ,const wchar_t* >::type     dft (L"%i");
    // doesn't work (error : type is not a member of std::enable_if< ... > !

    std::basic_string< T ,std::char_traits < T > > frmt ((pszFrmt && (*pszFrmt)) ? pszFrmt : dft );

    str_printf( szBuff ,iLen ,frmt.c_str() ,iNum );

    return szBuff;
}

////////////////////////////////////////////////////////////////////////////////

Can I do this in a simillar way or is the working version the best way ?
Or how to do this >

I don’t need suggestion to use stringstreams (that’s not what this question is about).

Using MSVS 2010 (and sorry ,no boost).

Thank you.

  • 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-19T14:40:39+00:00Added an answer on May 19, 2026 at 2:40 pm

    Frankly, this is the only solution I can think of:

    template <typename T> struct Dft { static const T* value; };
    template <> const char* Dft<char>::value = "%i";
    template <> const wchar_t* Dft<wchar_t>::value = L"%i";
    
    template < typename T ,typename I > 
    inline 
    std::basic_string< T ,std::char_traits < T > > 
    to_string ( I iNum ,const T* pszFrmt )
    {
        const int iLen (65);
        T szBuff [iLen] = {0};
    
        std::basic_string< T ,std::char_traits < T > > frmt ((pszFrmt && (*pszFrmt)) ? pszFrmt : Dft<T>::value );
    
        str_printf( szBuff ,iLen ,frmt.c_str() ,iNum );
    
        return szBuff;
    };
    

    It’s not pretty, but it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to expand my sons interest from Warcraft 3 programming into C++ to
I'm trying to expand a minified CSS file (don't ask) to make it human
I am trying to expand from php. Here is a basic problem I have
I've been trying to fix this problem almost all day (or it feels like
I am currently trying to design a website template for me, the problem is
After few weeks break, I'm trying to expand and extend my knowlege of templates
I'm trying to expand navigation options of the context menu on certain elements (specifically,
I'm currently trying to expand my PHP driven intranet site for my company. It
I have a table column I’m trying to expand and hide. jQuery seems to
I am trying to make my DIV's height expand to it's child elements. I

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.