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 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

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you're referring to is the data URI scheme, which… May 17, 2026 at 2:07 am
  • Editorial Team
    Editorial Team added an answer I actually found a work around to this that involved… May 17, 2026 at 2:07 am
  • Editorial Team
    Editorial Team added an answer What about when there are three e-mails/names? With shown data… May 17, 2026 at 2:07 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am in the process of learning C++ and came across an article on
I am learning Python and came across this example: W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6)) b =
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
i started out learning cocos2d and came across these lines of code: -(id)init {
I'm learning queues and I came across this piece of code. It's from a
Hy, I'm learning for my exams and came over the following question: Take the
When MVC first came out, I was reading about it everyday and learning all

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.