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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:48:35+00:00 2026-05-25T20:48:35+00:00

Working on a simple example for template functions. The code compiles and works as

  • 0

Working on a simple example for template functions. The code compiles and works as expected. But my question is why “static” is required in both “Cmp” and “Lit”? Otherwise, it will not compile?

Thanks a lot!

template<class T> class Cmp{
public:
    static int work(T a, T b) {
        std::cout << "Cmp\n";
        return 0;
    }
};

template<class T> class Lit{
public:
    static int work(T a, T b){
        std::cout << "Lit\n"  ;   
        return 0;
    }
};

template<class T, class C>
int compare(const T &a, const T &b){
    return C::work(a, b);
}


void test9(){
    compare<double, Cmp<double> >( 10.1, 20.2);
    compare<char, Lit<char> >('a','b');
}
  • 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-25T20:48:36+00:00Added an answer on May 25, 2026 at 8:48 pm

    The reason that static is required here is that in the compare template function, you have this line:

    return C::work(a, b);
    

    The syntax C::work(a, b) here means “call the function work nested inside the class C. Normally, this would try to call a member function without providing a receiver object. That is, typically the way you’d call a function work would be by writing

    C myCObject;
    myCObject.work(a, b);
    

    In this case, though, we don’t want to be calling a member function. Instead, we want the function work to be similar to a regular function in that we can call it at any time without having it act relative to some other object. Consequently, we mark those functions static so that they can be called like regular functions.

    Hope this helps!

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

Sidebar

Related Questions

Here is a very simple example in Java intended to print out all of
I have an extremely simple example trying to use Data Annotations with unobtrusive javascript
I have made a program with a scroll pane, but it is not working.
I want to develop an authentication module using PAM, but I'm having trouble getting
I'm new to VBA but I'm trying to get a DYMO LabelWriter to work
I am not new to programming, but kind of rusty in Javascript/web development. I
How can I include an if statement in my loop conditional in XSLT? I
I've spent the last day or so looking in CAS and have managed to
I ran into problems when trying to xml serialize a class to xml after
We are planning to upgrade our application to Rails3. One plugin we've used quite

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.