f



How to define a template function parameterized by type of function? Accelerated C++ exercise 8-1

It's from the Accelerated C++ exercise 8-1

The old version is based on pointer to function:

double analysis(const std::vector<Student_info>& students,
				double analysis_grade(const Student_info& s))
{
	vector<double> grades;

	transform(students.begin(), students.end(),
		back_inserter(grades), analysis_grade);
	return median(grades);
}

void write_analysis(ostream& out, const string& name,
					double analysis(const std::vector<Student_info>& students,
					double analysis_grade(const Student_info& s)),
					double analysis_grade(const Student_info& s),
					const vector<Student_info>& did,
					const vector<Student_info>& didnt)
{
	out << name << ": median(did) = " << analysis(did, analysis_grade) <<
		", median(didnt) = " << analysis(didnt, analysis_grade) << endl;
}

// call the functions
write_analysis(cout, "median", analysis, grade_aux, did, didnt);
write_analysis(cout, "average", analysis, average_grade, did, didnt);
write_analysis(cout, "median of homework turned in",
		analysis, optimistic_median, did, didnt);

The only difference is the grading function:
grade_aux, average_grade, and optimistic_median.

The question ask me to write a template function,
parameterized by the type of grading function.

How can I do that?

The book doesn't give an example, where can I find more information
about that?
0
4/27/2008 8:17:05 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

0 Replies
822 Views

Similar Articles

[PageSpeed] 29

Reply:

Similar Artilces:

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

C.8.16.1 Image Type and Frame Type vs C.8.16.1.4 Derived Pixel Contrast
There is a small inconsistency in between =A7C.8.16.1 Image Type and Frame = Type and =A7C.8.16.1.4 Derived Pixel Contrast: [...] Image Type (0008,0008) and Frame Type (0008,9007) shall consist of four non= -zero length values. [...] while [...] Value 4 of Image Type (0008,0008) and Value 4 of Frame Type (0008,9007) sha= ll not be zero length unless the SOP Class UID is "1.2.840.10008.5.1.4.1.1.= 2.2" or "1.2.840.10008.5.1.4.1.1.4.4" or "1.2.840.10008.5.1.4.1.1.128.1" (L= egacy Converted). [...] Thanks Mathieu I will put in a CP about this. ...

How to pass C++ member function to C function expecting function pointer?
I'm just starting to use C++, and don't want to have to rewrite all of the old C routines that I've been using for 20+ years. One of these is a general purpose function minimizer, which expects to be passed a pointer to the function to be minimized, double func(double p[]) How do I use this to minimize a function defined within a class in C+ +? To be specific, suppose I define the following class, which returns the sum of squared differences between an input vector and a (private) vector called trueParms: class max_func { public: max_func(vector <double> ...

Setting a C++ function as callback to a C Function!
Hi All, I have a C interface which takes a C function pointer as input for setting callback. Now, I want to set a C++ function for the same. I am getting a calling convention error. Is it possible for me to do this? Any pointers in that direction would be of great help. Thanks in Advance! Regards, Jayaraghavendran.K On 19 Jan., 19:36, Jay <jayaraghavend...@gmail.com> wrote: > Hi All, > > I have a C interface which takes a C function pointer as input for > setting callback. Now, I want to set a C++ function for the same. > > I am getting a calling convention error...

Passing a C++ object's member function to a C function expecing a function pointer!
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an external C struct (external to the C++ project/classes etc.) which is wants a function ptr assigned to one of it's members: struct blah { int (func_ptr*) (int a, int b, float c); }; etc... However, my code is in C++ and a wish to assign a non-static member function (I know I can do this) to this struct member! Can this be done!? Some how with pointers or references? What about the STL's mem_func stuff? Can anyone shed some light on this? Cheers Jim James, "James Vanns" <jimv@canterbury.a...

templated functions call C functions
Hi all, I'm trying to use Lapack's xGESVD functions in my C++ code. What I want to be able to do is create one function in C++ that will call the correct Lapack function depending on it's parameters. So something like this: template<class T> int svd(std::valarray<T>& A,...) { // somehow call the correct svd function here // like this: if(std::numeric_limits<T>::epsilon()==std::numeric_limits<double>::epsilon()) { dgesvd(...) } else { sgesvd(...) } //take care of the complex cases too, maybe with even more advanced...

Invoking c function in a c plus plus function...
Hi Everyone, I want to invoke a c function ( object file created by cc ) from a cpp function ( object file is created using g++ ). When i try to link, both the object files using g++ g++ cfile.o cppfile.o i get an error saying, : undefined reference to `fun()' collect2: ld returned 1 exit status Note that the fun() is the c function defined in cfile.c. I can't use cc to link both the object files as the master file is a cpp source file. Is there anyway to get this done? I know the method of using extern "C" but that works only when a cpp function ( object file cre...

c function in c++
error LNK2019: unresolved external symbol __imp__fwrite referenced in function "void __stdcall dg729(void)" (?dg729@@YGXXZ) error LNK2001: unresolved external symbol __imp__fwrite how to use .c function in c++ code mathudiv@gmail.com wrote: > error LNK2019: unresolved external symbol __imp__fwrite referenced in > function "void __stdcall dg729(void)" (?dg729@@YGXXZ) > > error LNK2001: unresolved external symbol __imp__fwrite > > how to use .c function in c++ code Make sure it is declared extern "C". -- Ian Collins. mathudiv@gmail.com wro...

F77-C: Passing Fortran FUNCTION as an argument for a C Function
Hello, I have a Fortran FUNCTION f1, a Fortran SUBROUTINE s1 and a C function c1. c1 has following prototype: void c1(double *,double (*fkt)(double,double*)); Now i want to CALL c1 from s1 and pass f1 as the second argument for c1. Is it possible? If yes, then how should i define c1 interface in Fortran? And how do i call c1 then? tia, schamil Schamil Wackenhut wrote: > Hello, > > I have a Fortran FUNCTION f1, a Fortran SUBROUTINE s1 and a C function > c1. > > c1 has following prototype: > > void c1(double *,double (*fkt)(double,double*)); > > Now i wa...

C++ typed interface to hundreds of C functions
Hi there, I'm handling pointers to "objects" described by C-style struct (e.g. Base), and I manipulate them through a low level C API. Sometimes a third-party binary function is applied to two of these, giving a pointer to a third. extern "C" Base *foo1_A1_B1_C1(Base *, Base *); extern "C" Base *foo1_A1_B1_C2(Base *, Base *); extern "C" Base *foo1_A1_B2_C1(Base *, Base *); ... etc. (hundreds more) I'd like to provide a better interface to this using class templates for the A,B and C parts: Derived<A,B,C> *foo1(Derived<A,B,C> &a...

Calling Lisp Function from C: How to declare this function in C land ?
Hi all: I have a lisp image that contains the following functions: (defun frgo (a) (format *debug-io* "~%~A~&" a) (values)) (cffi:defcallback frgo_helper :void ((a-cstr :string)) (frgo (cffi:foreign-string-to-lisp a-cstr))) In a C dynamic library I have: * file frgo_c.h: extern void frgo_helper( char * a ); * file frgo_c.c: void frgo( char * a ) { frgo_helper( a ); } Now, the library does not compile - undefined symbol _frgo_helper() ! How do I make functions "known" to C land that are purely defined as callbacks in Lisp lan...

How to impliment searching the definition of a c/c++ function in a c/c++ source file
How to impliment searching the definition of a c/c++ function in a c/c+ + source file? are there any libs of opensource to use? I have no ideal about it, and any one who responses it will be appreciated. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] On Jun 18, 5:25 am, whiteid...@gmail.com wrote: > How to impliment searching the definition of a c/c++ function in a c/c+ > + source file? are there any libs of opensource to use? I have no > ideal about it, and any one who responses it will be a...

BIND(C) INTERFACE for C function with internal C macro in the C API
Hi, I'm trying to figure out what my Fortran interface (with BIND(C)) for the code below should be. It gives the correct value in the C API for length of the string passed to C if I remove the BIND(C) statement. But when I add the BIND(C) statement it gives garbage for 'length', I tried to add an OPTIONAL INTEGER where STR_PLEN() is given but that just gives length=0. Thanks for the help. FORTRAN ============================ (using gfortran 4.8.2) MODULE test USE ISO_C_BINDING INTERFACE SUBROUTINE open_f(filename) BIND(C,NAME="open_f_") ...

Web resources about - How to define a template function parameterized by type of function? Accelerated C++ exercise 8-1 - comp.lang.c++

Parameterized post-Newtonian formalism - Wikipedia, the free encyclopedia
Post-Newtonian formalism is a calculational tool that expresses Einstein's (nonlinear) equations of gravity in terms of the lowest-order deviations ...

Give me parameterized SQL, or give me death
I have fairly strong feelings when it comes to the stored procedures versus dynamic SQL argument , but one thing is clear: you should never, ...

3D Printing of Parameterized Speaker Enclosures
Despite what you would gather from looking at a mess of wires, carpet, and MDF in the back of a Honda Civic hatchback, building speaker enclosures ...

miniSynth PRO for iPad on the iTunes App Store
Read reviews, get customer ratings, see screenshots, and learn more about miniSynth PRO on the App Store. Download miniSynth PRO and enjoy it ...

Involver introduces Engagement Optimization API to be used by any Facebook ad provider
... tools] should interoperate with each other.” When businesses develop applications using Involver’s Social Markup Language , they can get parameterized ...

► October (1)
Particle Physics Blog Tuesday, 30 October 2007 Holographic Baryons Last week Deog Ki Hong was explaining how baryons can be realized in holographic ...

Cute Little Abstractions
... { CountingSet types = new CountingSet = 3; } My client now only contains intent. The small ugliness with the parameterized map and boxing is ...

Google Releases Chart Image Generator
... a chart generator service they are calling the Google Chart API . Usage is quite straightforward: you link to an image in the form of a parameterized ...

It's not just reuse
In software development, there are two kinds of reuse: reference-reuse and fork-reuse. Reference-reuse is what we usually understand for mere ...

Physics Envy in Finance
This represents my personal opinion, not the views of the SEC or its staff. If all you have is a hammer... I read a New York Times article ...

Resources last updated: 1/28/2016 2:19:25 PM