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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:35:57+00:00 2026-06-06T23:35:57+00:00

I am deep into a new project which I address with a bunch of

  • 0

I am deep into a new project which I address with a bunch of templates and specializations of them. Now, after a day without programming, I find myself asking whether it is really worth the extra lines of code.

The question is: What are the advantages of specialization?

Is this:

template <int i> class A {};
template <> class A <1> { void foo() {/* something */}  };
template <> class A <2> { void foo() {/* something else*/}  };
template <> class A <3> { void foo() {/* even different*/}  };

In any way better (faster execution) than

template <int i> class A { 
   void foo() {
      if (i==1) {/* something */}
      else if (i==2) {/* something else*/}
      else if (i==3) {/* even different*/}
   }
};

?

Edit:

The code is part of a library to be used by others.
I am using gcc 4.6.3 but in the end the code will be used with different compilers.

Edit:

These two pieces of code result in identical binaries using gcc 4.6.3. I cannot test the full case as my actual code is far from being usable. It really seems to be a matter of principle, versatiliy, reusability, maintanability etc…

  • 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-06T23:35:59+00:00Added an answer on June 6, 2026 at 11:35 pm

    Speed is not the primary issue here, but extensibility is.

    Specialization has the advantage that you make it easier for clients of your code to add new overloads of foo(). Suppose you later decide to add new behavior for i=4: in the first approach, you simply add a new specialization; in the second version, you need to modify the function foo(). If you have released your library in binary form, clients will not be happy.

    The preference of the specialization approach to the second one is a manifestation of the Open/Closed Principle: code should be open for extension, closed for modification.

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

Sidebar

Related Questions

...Maybe imperative programming with mutable data is just drilled too deep into my brain,
I'm fairly deep with Flash CS4 project. I've got Flex Builder now, and I
I am just starting to model the data for a new C# project, which
I'm deep into a MFC project that I originally didn't think needed a toolbar,
We're neck deep in a project right now, schedules are tight (but reasonable). Our
I'm starting a new project with Spring 3.1, and have been eyeball deep in
I'm currently trying to dig deep into python and I have found a challenge
I have a TreeStore that you can drill into. It's only two levels deep.
Greetings, I'm trying to develop some tests for Mason components which requires running them
I am quite new to Cocoa and to iPhone programming. I am using the

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.