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

The Archive Base Latest Questions

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

I have a template member function in a class that is called for all

  • 0

I have a template member function in a class that is called for all bool, double, int and string. I want to carry out few instructions that are common to all the above mentioned data types. But for String last few lines of code is different. So can any one suggest me a better way to carry out this in a same template function.

template< class T>

xyz (t* a)

{
       //few lines are common for all types for data

       //last 3 lines of code is different for Strings
}
  • 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-23T14:15:04+00:00Added an answer on May 23, 2026 at 2:15 pm

    The solution, as often, is to factorize the common behavior and provide a mean to specialize some parts of the algorithm (see the Template Method pattern).

    Here, you can do this quite easily by moving the last lines of your function in a function of its own, which can be specialized for certain data types. Remember that when it comes to functions, overloading should be preferred to template specialization.

    template <class T>
    void xyz(T * a)
    {
        //few lines are common for all types for data
    
        xyz_finish(a);
    }
    
    template <class T>
    void xyz_finish(T * a)
    {
        // default case (can be empty)
    }
    
    void xyz_finish(std::string * s)
    {
        // string case
    }
    

    Of course, your function should have a more descriptive name than the one I used…

    You can also do the symmetrical operation: move the common behavior in a function, and overloads the “top-level” function:

    template <class T>
    void xyz(T * a)
    {
        common_behavior(a);
    }
    
    void xyz(std::string * s)
    {
        common_behavior(s);
    
        // code specific to strings
    }
    
    template <class T>
    void common_behavior(T * a)
    {
        //few lines that are common for all types for data
    }
    

    If you do not want or cannot create other functions, you can test the type of the parameter:

    template <class T>
    void xyz(T * a)
    {
        // common code
    
        if (is_same<T, std::string>::value)
        {
            //code for strings
        }
    }
    

    is_same is a class template containing a value which is true if its two parameters are the same type, available in TR1, Boost and C++0x. This solution will work only if the code in the if clause is valid for every data types you instantiate the template with. For example, if you use a member function of string in the if block, compilation will fail when instantiating the function with the other data types, since you cannot invoke a method on a primitive type.

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

Sidebar

Related Questions

I have a template class that has a template member function that needs to
Lets say you have simple template function (not class member for the sake of
I have a template class where I want to use objects of that class
I have a problem with class member function templates. It seems that gcc is
I have a member function of a template class declared as such: template <class
I have a templated class with an templated member function template<class T> class A
I have a class template Foo<T> . I'd like to implement a non-member function
Suppose I have a class called Object. The Object class has a member function
I wonder if it is a good practice to have a member template function
I have a template class that I serialize (call it C), for which 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.