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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:14:09+00:00 2026-05-12T10:14:09+00:00

right now i am learning C++, and now I know the basic concept of

  • 0

right now i am learning C++, and now I know the basic concept of template,
which act just like a generic type,
and i found almost every c++ program used template,
So i really want to know when are we supposed to use template ?
Can someone conclude your experience for me about c++ template ?
When will you consider to use template ?

Supplement:
if we defined such function

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

but we want to pass a object(self-defined class) for comparison, how can we implement ?

Supplement2:
in the answer below, someone have wrote this sample code

template <class myType>
const myType& GetMax (const myType& a, const myType& b) {
    return (a<b?b:a);
}

template <class myType, class Compare>
const myType& GetMax (const myType& a, const myType& b, Compare compare) {
    return (compare(a,b)?b:a);
}

is this correct ? can we just pass a function name as a parameter of class myType ?

  • 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-12T10:14:09+00:00Added an answer on May 12, 2026 at 10:14 am

    Re: supplement. If you want to pass a comparison function, you could provide another overload:

    template <class myType>
    const myType& GetMax (const myType& a, const myType& b) {
        return (a<b?b:a);
    }
    
    template <class myType, class Compare>
    const myType& GetMax (const myType& a, const myType& b, Compare compare) {
        return (compare(a,b)?b:a);
    }
    

    Samle usage: to compare C-style strings:

    bool c_strings_less(const char* a, const char* b)
    {
        return std::strcmp(a, b) < 0; //is a less than b
    }
    
    const char* greater = GetMax("hello", "world", c_strings_less);
    

    This is how the std::max algorithm works. (I also made a few modifications, e.g it is customary in C++ that predicates define “less-than” comparison.)

    Or if you asked, how GetMax would work for arbitrary user-defined types, then those must overload operator> or your function would result in a compile error.

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

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 163k
  • 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 Here is an extension method that will do what you… May 12, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer Names like "Tue" and "Sep" are English. Other languages use… May 12, 2026 at 12:06 pm
  • Editorial Team
    Editorial Team added an answer Use two stacks. One is the data, one is the… May 12, 2026 at 12:06 pm

Related Questions

I'm writing a merge sort function, and right now I am just using a
I'm learning python for a couple of days now and am struggling with its
I want to set up a continuous integration and test framework for my open
Is there any way to debug a single file in Visual Studio.NET? I'm still
I am an experienced C++ programmer with average Python skills. The reasons I studied

Trending Tags

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

Top Members

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.