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

The Archive Base Latest Questions

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

Template methods as in NOT C++ templates . So, say that you would like

  • 0

Template methods as in NOT C++ templates.

So, say that you would like to do some searching with different algorithms – Linear and Binary for instance. And you would also like to run those searches through some common routines so that you could, for instance, automatically record the time that a given search took and so on.

The template method pattern fills the bill beautifully. The only problem is that as far as I’ve managed to dig around, you can’t actually implement this behaviour via static methods with C++, ’cause you would also need to make the methods virtual(?) Which is of course a bit of a bummer because I don’t have any need to alter the state of the search object. I would just like to pin all the searching-thingies to its own namespace.

So the question is: Would one want to use something like function/method pointers instead? Or would one just use namespaces to do the job?

It’s pretty hard to live with this kind of (dare I say) limitations with C++, as something like this would be a breeze with Java.

Edit:

Oh yeah, and since this is a school assignment, the use of external libraries (other than STL) isn’t really an option. Sorry for the hassle.

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

    I don’t see why you’d need the template method pattern.

    Why not just define those algorithms as functors that can be passed to your benchmarking function?

    struct BinarySearch { // functor implementing a specific search algorithm
      template <typename iter_type>
      void operator()(iter_type first, iter_type last){ ....}
    };
    
    template <typename data_type, typename search_type>
    void BenchmarkSearch(data_type& data, search_type search){ // general benchmarking/bookkeeping function
    // init timer
    search(data);
    // compute elapsed time
    }
    

    and then call it like this:

    int main(){
      std::vector<int> vec;
      vec.push_back(43);
      vec.push_back(2);
      vec.push_back(8);
      vec.push_back(13);
    
      BenchmarkSearch(vec, BinarySearch());
      BenchmarkSearch(vec, LinearSearch()); // assuming more search algorithms are defined
      BenchmarkSearch(vec, SomeOtherSearch();
    }
    

    Of course, another approach, which is a bit closer to what you initially wanted, could be to use CRTP (A pretty clever pattern for emulating virtual functions at compile-time – and it works with static methods too):

    template <typename T>
    struct SearchBase { // base class implementing general bookkeeping
      static void Search() {
        // do general bookkeeping, initialize timers, whatever you need
        T::Search(); // Call derived search function
        // Wrap up, do whatever bookkeeping is left
      }
    }
    
    
    struct LinearSearch : SearchBase<LinearSearch> // derived class implementing the specific search algorithms
    {
      static void Search(){
        // Perform the actual search
      }
    };
    

    Then you can call the static functions:

    SearchBase<LinearSearch>::Search();
    SearchBase<BinarySearch>::Search();
    SearchBase<SomeOtherSearch>::Search();
    

    As a final note, it might be worth mentioning that both of these approaches should carry zero overhead. Unlike anything involving virtual functions, the compiler is fully aware of which functions are called here, and can and will inline them, resulting in code that is just as efficient as if you’d hand-coded each case.

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

Sidebar

Ask A Question

Stats

  • Questions 294k
  • Answers 294k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i have solved it my self.. var newElement = new… May 13, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer if you tired of using standard like (IF, WHILE), here… May 13, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer I answer my own question. I think that the better… May 13, 2026 at 6:35 pm

Related Questions

Let's say you have a class class C { int * i; public: C(int
Important: This question is getting quite long, if this is the first time you're
Pimpl's are a source of boilerplate in a lot of C++ code. They seem
I have a template class where I want to use objects of that class

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.