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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:15:35+00:00 2026-05-21T14:15:35+00:00

Possibly easy to solve, but its hard to find a solution to this: Is

  • 0

Possibly easy to solve, but its hard to find a solution to this:

Is it possible to (partially) specialize for a whole set of types?
In the example “Foo” should be partially specialized for (T,int) and (T,double) with only one template definition.

What I can do is define a specialisation for (T,int). See below. But, it should be for (T,int) and (T,double) with only one function definition (no code doubling).

template <typename T,typename T2>
struct Foo
{
  static inline void apply(T a, T2 b) 
  {
    cout << "we are in the generic template definition" << endl;
  }
};

// partial (T,*)
template <typename T>
struct Foo<T, int >     // here something needed like T2=(int, double)
{
  static inline void apply(T a, T2 b) 
  {
    cout << "we are in the partial specialisation for (T,int)" << endl;
  }
};

Any ideas how to partially specialize this for (T,int) and (T,double) with one template definition?

  • 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-21T14:15:36+00:00Added an answer on May 21, 2026 at 2:15 pm

    If I understood your question correctly, then you can write a base class template and derive from it, as illustrated below:

    template <typename T, typename U>
    struct Foo_Base
    {
      static inline void apply(T a) 
      {
        cout << "we are in the partial specialisation Foo_Base(T)" << endl;
      }
    };
    
    template <typename T>
    struct Foo<T, int> : Foo_Base<T, int> {};
    
    template <typename T>
    struct Foo<T, double> : Foo_Base<T, double> {};
    

    Although its not one template definition (as you asked for), but you can avoid the code duplication.

    Demo : http://www.ideone.com/s4anA

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

Sidebar

Related Questions

This particular problem is easy to solve, but I'm not so sure that the
I'm wondering whether something like this is possible (and relatively easy to do), and
I am coming from c# where this was easy, and possible. I have this
I'm possibly just stupid, but I'm trying to find a user in Active Directory
I'm very sure this problem has been solved, but I can't find any information
This is easy to work around, but I just curious if I could be
I find myself stuck in something I think would be really easy to solve.
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
does anyone know how easy it is or how possible it is to skin
Is it possible to use C/C++ code in Android? And how easy is it?

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.