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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:10:15+00:00 2026-05-25T10:10:15+00:00

Briefly dismiss the fact that normal function overloading will serve this example better. It

  • 0

Briefly dismiss the fact that normal function overloading will serve this example better. It is meant only as a way to learn about template programming. Having said that, you are welcome to comment on the benefits/differences you’ll get from using function overload, compared to function template specialization (although that might deserve a question of its own).


Consider the following example:

template <typename T>
inline void ToString(T value, char* target, size_t max_size );

template <>
inline void ToString<float>(float value, char* target, size_t max_size)
{
   snprintf( target , max_size , "%f" , value);
}

template <>
inline void ToString<double>(double value, char* target, size_t max_size)
{
    snprintf( target , max_size , "%f" , value);
}

Is there a way to write only one of these specializations that match both float and double types?

Basically I envision writing a template specialization for a template type that will match both float and double (as sort of ‘float or double’ type matcher) but I’m not sure whether that is possible at all with C++. That said, I’ve seen unexpected template magic happen in front of my eyes before, so I think it’s a good question to ask here.

  • 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-25T10:10:16+00:00Added an answer on May 25, 2026 at 10:10 am

    Here’s a standard solution idiom:

    #include <type_traits>
    #include <cstdio>
    
    
    // Helper class
    
    template <typename T>
    struct Printer
    {
      static typename std::enable_if<std::is_floating_point<T>::value, int>::type
      print(T x, char * out, std::size_t n)
      {
        return std::snprintf(out, n, "%f", x);
      }
    };
    
    // Convenience function wrapper
    
    template <typename T> int print(T x, char * out, std::size_t n)
    {
      return Printer<T>::print(x, out, n);
    }
    
    void f()
    {
      char a[10];
    
      Printer<double>::print(1.2, a, 10);  // use helper class
      print(1.4f, a, 10);                  // wrapper deduces type for you
    }
    

    You’ll get a compile-time error if you call either construction with a non-floating type. Beware though that this might erroneously work for long doubles, which require the %Lf format specifier; and also recall that floats get promoted to doubles when passed through variadic function arguments.

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

Sidebar

Related Questions

I've followed this example: RCP+JavaWS but the app just briefly comes up and goes
This is only very briefly, while the loading of elements and jsp includes occurs.
I ran the following simple query that looks like the example below: (Briefly; one
Briefly: Does anyone know of a GUI for gdb that brings it on par
I'm working on a database that tracks files and dependencies in projects. Briefly, I
(Briefly, like this question but for Windows servers.) I have several Win2003 servers running
Briefly described, I have 2 tables that have 'equivalent' rows in each other. The
As briefly discussed in this post and in a comment by the same author
As mentioned briefly in this article: http://en.wikipedia.org/wiki/Delta_encoding#Delta_encoding_in_HTTP which is referenced from here: https://www.rfc-editor.org/rfc/rfc3229 I'm
Briefly: Is there a way to post images to Picasa or Flickr from my

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.