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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:52:13+00:00 2026-06-15T14:52:13+00:00

Is it possible to make a template for a function that has a template

  • 0

Is it possible to make a template for a function that has a template class in its argument list?

I would like to make one template for statSelection() and statInsertion() that would allow me to test different sorting algorithms without having to create a separate stat function for each type of sorting algorithm I am testing. (My sorting algorithms are template classes)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "FileGen.h"
#include "FileRead.h"
#include "SelectionSort.h"
#include "SelectionSort.cpp"
#include "InsertionSort.h"
#include "InsertionSort.cpp"

using namespace std;

void statSelection(int[], int[], Selection<int>, Selection<int>);
void statInsertion(int[], int[], Insertion<int>, Insertion<int>);

int main () 
{
    FileGen fileGen;
    FileRead fileRead;
    Selection<int> selectHundred;
    Selection<int> selectThousand;
    Insertion<int> insertionHundred;
    Insertion<int> insertionThousand;
    int valuesHundred[100];
    int valuesThousand[1000];
    fileGen.generateFiles();
    fileRead.readFiles(valuesHundred, valuesThousand);
    statSelection(valuesHundred, valuesThousand, selectHundred, selectThousand);
    fileGen.generateFiles();
    fileRead.readFiles(valuesHundred, valuesThousand);
    statInsertion(valuesHundred, valuesThousand, insertionHundred, insertionThousand);
    system("pause");
    return 0;
}

void statSelection(int vHundred[], int vThousand[], Selection<int> sHundred, Selection<int> sThousand)
{
    cout << "One Hundred Items" << endl;
    sHundred.SelectionSort(vHundred, 100);
    sHundred.selectionSortPreformance();
    cout << "One Thousand Items" << endl;
    sThousand.SelectionSort(vThousand, 1000);
    sThousand.selectionSortPreformance();
}

void statInsertion(int vHundred[], int vThousand[], Insertion<int> iHundred, Insertion<int> iThousand)
{
    cout << "One Hundred Items" << endl;
    iHundred.InsertionSort(vHundred, 100);
    iHundred.insertionSortPreformance();
    cout << "One Thousand Items" << endl;
    iThousand.InsertionSort(vThousand, 1000);
    iThousand.insertionSortPreformance();
}
  • 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-06-15T14:52:13+00:00Added an answer on June 15, 2026 at 2:52 pm

    I would rather use polymorphism. (The solution without polymorphism can be found after a horizontal rule)

    I would inherit both Insertion<_Tp> and Selection<_Tp> from an abstract class (interface) called ISortable<_Tp>, and name .InsertionSort and .SelectionSort member functions simply as .Sort (which would be a virtual member function of Sortable<_Tp>).

    template<typename _Tp>
    class ISortable<_Tp>{
    public:
        virtual void Sort(_Tp *, int)=0; // btw functions are usually lowercase
        virtual void Performance()=0; 
    };
    
    template<typename _Tp>
    class InsertionSort<_Tp> : public Sortable<_Tp>{
    //...
        virtual void Sort(_Tp *, int); 
        virtual void Performance(); 
    };
    //...
    

    So your function can be written like this:

    void statSelection(int[], int[], Sortable<int>&, Sortable<int>&);
    
    void statSelection(int[], int[], Sortable<int>&sHundred, Sortable<int>&)
    {
    //...
      sHundred.Sort(vHundred, 100);
      sHundred.Performance();
    //...
    }
    

    Solution without polymorphism:

    It is possible to do, just name BOTH your sort and performance functions with the same name.

    Then

    template<typename _Tp_sortable>
    void statGeneral(int[], int[], _Tp_sortable sHundred, _Tp_sortable)
    {
    //...
      sHundred.Sort(vHundred, 100);
      sHundred.Performance();
    //...
    }
    

    The examples: (Im not sure if you actually need the <Selection<int> > part after the function, but I’d call it with it.)

    statGeneral<Selection<int> >(valuesHundred, valuesThousand, selectHundred, selectThousand);
    statGeneral<Insertion<int> >(valuesHundred, valuesThousand, insertionHundred, insertionThousand);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to make a class that has a template object member inside
is it possible to somehow make a partial template specification a friend class? I.e.
Possible Duplicate: Template deduction for function based on its return type? I am trying
I know it's possible to make a template function: template<typename T> void DoSomeThing(T x){}
I need to make a template function that receives as parameter a std::container of
I have two functions and i want to make it like one. function 1
In other words: Is it possible to make a template specialisation that inherits from
I want to make a class that will have a single get template method
Is it possible make some handler that will do something when user shutdown computer
Is it possible make an activity, that will lock the android device? That device

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.