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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:58:36+00:00 2026-05-16T17:58:36+00:00

Learning C++, came upon function templates. The chapter mentioned template specialization. template <> void

  • 0

Learning C++, came upon function templates. The chapter mentioned template specialization.

  1. template <> void foo<int>(int);

  2. void foo( int );

Why specialize when you can use the second? I thought templates were suppose to generalize. What’s the point of specializing a function for a specific data type when you can just use a regular function?

Obviously, template specialization exists for a reason. When should it be used? I read Sutter’s “Why not Specialize…” article but I need more of a layman’s version since I’m just learning this stuff.

  • 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-16T17:58:37+00:00Added an answer on May 16, 2026 at 5:58 pm

    The main difference is that in the first case you are providing the compiler with an implementation for the particular type, while in the second you are providing an unrelated non-templated function.

    If you always let the compiler infer the types, non-templated functions will be preferred by the compiler over a template, and the compiler will call the free function instead of the template, so providing a non-templated function that matches the arguments will have the same effect of specializations in most cases.

    On the other hand, if at any place you provide the template argument (instead of letting the compiler infer), then it will just call the generic template and probably produce unexpected results:

    template <typename T> void f(T) { 
       std::cout << "generic" << std::endl; 
    }
    void f(int) { 
       std::cout << "f(int)" << std::endl; 
    }
    int main() {
       int x = 0;
       double d = 0.0;
       f(d); // generic
       f(x); // f(int)
       f<int>(x); // generic !! maybe not what you want
       f<int>(d); // generic (same as above)
    }
    

    If you had provided an specialization for int of the template, the last two calls would call that specialization and not the generic template.

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

Sidebar

Related Questions

I'm learning OO stuff, and came across this: class n{ private function f($v){ return
I'm completely new to rails. While learning I came across the use of the
While learning to use LINQ in VB.NET, I came across the following: Dim x
I'm new to rails and am learning how to use it. I came across
I'm learning how to use Nokogiri and few questions came to me based on
I am learning EF and came across the definition of a Complex Type: ...used
I'm learning C++ and came across those *.lib files that are obviously used by
I am learning JSF and came across this line: <h:messages layout=table></h:messages> in a sample
So I have been learning assembly and came to the topic of stack, storing
in the book i'm learning from i came across this code snippit: while (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.