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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:02:26+00:00 2026-05-20T05:02:26+00:00

In the below code snippet, template<typename T1> void func(T1& t) { cout << all

  • 0

In the below code snippet,

template<typename T1>
void func(T1& t)
{
    cout << "all" << endl;
}

template<typename T2>
void func(T2 &t)
{
    cout << "float" << endl;
}

// I do not want this
// template<> void func(float &t)

int main()
{
    int i; float f;
    func(i); // should print "all"
    func(f); // should print "float" 
    return 0;
}

I would like to have the templates modified which by passing any type other than float will print “all” and passing float will print “float”. I do not want template specialization, instead have partial specialization which will act accordingly based on input type. How should i go about it. Thanks in advance.

Well the scenario, i’m currently facing is like,
I need to have the following defined,

template<typename T1>
void func(T1 &t)
{
    cout << "t1" << endl;
}

template<typename T2>
void func(T2 &t)
{
    cout << "t2" << endl;
}

The following calls should print “t2”

func(int) // print "t2"
func(float) // print "t2"
func(string) // print "t2"

The following calls should print “t1”

func(char) // print "t1"
func(xyz) // print "t1"
...
func(abc) // print "t1"

some kind of grouping like the above where few should call the partial specialization implementation and others should call the default implementation.

  • 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-20T05:02:26+00:00Added an answer on May 20, 2026 at 5:02 am

    Write a type traits class for your condition:

    template<class T>
    struct IsIntFloatOrString {
      enum { value = boost::is_same<T, int>::value
                  or boost::is_same<T, float>::value
                  or boost::is_same<T, string>::value };
    };
    

    Use boost::enable_if and disable_if:

    template<typename T1>
    typename boost::enable_if<IsIntFloatOrString<T1> >::type
    func(T1 &t) {
      cout << "t1" << endl;
    }
    
    template<typename T2>
    typename boost::disable_if<IsIntFloatOrString<T2> >::type
    func(T2 &t) {
      cout << "t2" << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I once saw this nice little snippet of code below, here at SO: template<typename
I am using the code snippet below, however it's not working quite as I
In the below code snippet can i replace char * to const char *
I have a table defined (see code snippet below). How can I add a
In the below code sample, what does {0:X2} mean? This is from the reflection
In the code snippet below, I only have 1 element in the XML that
In the below code snippet, the intention is to take value for name from
I am trying to add two Vectors below is the code snippet :- #include
Anyone have any idea why CGContextFillPath won't work in the code snippet below? I'm
A trick question about C pointer. Read code snippet below and try explain why

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.