Confused by template exercise

This is a discussion on Confused by template exercise within the C++ Programming forums, part of the General Programming Boards category; I'm confused by a question at the end of chapter 8 of "Accelerated C++." The question is: Note that the ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Confused by template exercise

    I'm confused by a question at the end of chapter 8 of "Accelerated C++." The question is:

    Note that the various analysis functions we wrote in 6.2/110 share the same behavior; they differ only in terms of the functions they call to calculate the final grade. Write a template function, parameterized by the type of the grading function, and use that function to evaluate the grading schemes.


    Here are the analysis functions in question:

    Code:
    double median_analysis(const vector<Student_info>& students)
    {
    	vector<double> grades;
    
    	transform(students.begin(), students.end(),
    	          back_inserter(grades), grade_aux);
    	return median(grades);
    }
    
    double average_analysis(const vector<Student_info>& students)
    {
    	vector<double> grades;
    
    	transform(students.begin(), students.end(),
    	          back_inserter(grades), average_grade);
    	return median(grades);
    }
    
    double optimistic_median_analysis(const vector<Student_info>& students)
    {
            vector<double> grades;
    
            transform(students.begin(), students.end(),
                      back_inserter(grades), optimistic_median);
            return median(grades);
    }
    I don't really know what the question means. The functions differ in the function they pass to "transform," but what does it mean to write a template function for these functions? When it mentions the "type" of the grading functions, does that mean the type they return? Each of the functions in question return a double. As do each of the above functions. So why would you write a template function? Surely if the object of the exercise is to consolidate the behavior of the three functions above into one, then wouldn't that just involve passing the function used by "transform" as an argument?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    >> but what does it mean to write a template function for these functions?

    Right. So calling the template function would look like:

    Code:
    double result = analysis(students, average_grade);
    Last edited by Sebastiani; 05-28-2009 at 10:30 PM. Reason: code tags



    ITSA
    Socket Library!

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by Sebastiani View Post
    >> but what does it mean to write a template function for these functions?

    Right. So calling the template function would look like:

    Code:
    double result = analysis(students, average_grade);
    But is that really calling a template function? Surely it's just a function which takes a function as an argument. The question comes at the end of a chapter about template functions, i.e. functions that have a template header and have type parameters. If it's all just a case of writing a function which takes a function as an argument then the question just seems a little simple in the context of the chapter it concludes, especially since such functions were covered (and quizzed on) two chapters earlier.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    The reason why templates are used in situations like this is because it is often useful to pass an object (a functor) instead of a 'simple' function, eg:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    #include <cmath>
    
    using namespace std;
    
    class generate_ascending_multiples
    {
    	public:
    	
    	generate_ascending_multiples( double initial = 0 )
    	: initial( initial ), value( initial )
    	{	}
    	
    	double operator ( )( double& data )
    	{
    		data = value;
    		value += initial;	
    		return data;
    	}
    	
    	double
    		initial, 
    		value;
    };
    
    int main( void )
    {
    	vector< double >
    		data( 32 );
    	for_each( data.begin( ), data.end( ), generate_ascending_multiples( exp( 1.0 ) ) );	
    	copy( data.begin( ), data.end( ), ostream_iterator< double, char >( cout, "\n" ) );
    	return 0;
    }
    Last edited by Sebastiani; 05-28-2009 at 11:53 PM. Reason: bad math



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21