`

c++ - a look at the function object and its predecessors

    博客分类:
  • c++
c++ 
阅读更多

this discuss the essence of using the functional object.

 

 

we will discuss the essence of using the functional object, starting from some template function. Then we examine the limitation of the template function, and then introduced a temporarily solution. ut later we proceed to the possibility of using a more advanced features which is called function object and how it solves the problem we have.

 

 

 

Let's first see the code, which has comments in between, hopefull this can help to portray the problem clearly.

 

/**
* functionalobjectessence.cpp
*
*  this discuss the essence of using the functional object
*
* we will discuss the essence of using the functional object, starting from some template function
* then we examine the limitation of the template function, and then introduced a temporarily solution
* but later we proceed to the possibility of using a more advanced features which is called function object and how it solves the problem we have
*/

#include "stdafx.h"
#include <iostream>
#include <functional>
#include <iterator>
#include <vector>
#include <list>


/*
* min
*
*  the limitation of the min function is there is constrain on the use of teh less-than operator. In one case, the underlying type may not support the less-than
*  operator, an image class, may not have the < operator built in
*/
template <typename Type>
const Type & 
min(const Type* p, int size) 
{
	int minIndex = 0;
	for (int ix = 1; ix < size; ++ix) { 
		if (p[ix] < p[minIndex] ) {
			minIndex = ix;
		}
	}
	return p[minIndex];
}

/**
* min
*
* while this solves the problem types not supported, it has a performance overhead that there is a indirect invocation, and there is no way to optimize away the performance overhead.
*/
template <typename Type, bool (*comp)(const Type &, const Type &)>
const Type &
min(const Type* p, int size) 
{
	int minIndex = 0;
	for (int ix = 1; ix < size; ++ix) { 
		if ( comp(p[ix], p[minIndex]) ) {
			minIndex = ix;
		}
	}
	return p[minIndex];

}

/**
* min
* 
* because of the limitation of providing a bool (*comp) (const Type &, const Type& ), here we try to provide a function object
* a function objec behaves like a function, but it is a class
* 
* the benefit of function object is two fold, first, the function call is a inline function, so ther is room for optimization; second, the 
*  object can hold an arbitrary amount of additional data, either cached results or data to help in current operation.
*
* another thing to note is that, you can still pass in a pointer to function, which can automatically deduced to type Comp;
*/
template <typename Type, typename Comp>
const Type&
min(const Type *, int size, Comp comp)
{
	int minIndex = 0;
	for (int ix = 1; ix < size; ++ix) { 
		if ( comp(p[ix], p[minIndex]) ) {
			minIndex = ix;
		}
	}
	return p[minIndex]; 
}
 

 

As you can see the function object has benefit of manifold.

 

let's first write down the source of functional objects

 

  • A set of arithmetic, relational , and logical function objects that is predefined by the standard libraries.
  • a set of predefined functional adapters allow us to specialized or extend predefined function object (or, for that matter, any function object)
  • we can defne our own function object. to be passed to hte generic algorithms and possibly against which to apply the function adapter.

 

分享到:
评论

相关推荐

    acpi控制笔记本风扇转速

    a mismatch between a 32-bit block address and the 64-bit X counterpart (when both are non-zero.) Example Code and Data Size: These are the sizes for the OS- independent acpica.lib produced by the ...

    Addison.Wesley.Real.Time.UML.Advances.in.The.UML.for.Real.Time.Systems.Third.Edition.eBook-DDU.chm

    Like its best-selling predecessors, Real Time UML, Third Edition, provides an overview of the essentials of real-time systems and an introduction to UML that focuses on the use of the ever-evolving ...

    Addison.Wesley.Exceptional.C++.Style.40.New.Engineering.Puzzles,.Programming.Problems.and.Solutio.chm

    This book takes a page from Socrates, as did its predecessors, Exceptional C++ [Sutter00] and More Exceptional C++ [Sutter02]. It assumes you're involved in some aspect of writing production C++ ...

    Windows Vista for Starters: The Missing Manual

    and much more &lt;br&gt;Windows Vista is a vast improvement over its predecessors, with an appealing, glass-like visual overhaul, superior searching and organization tools, a multimedia and ...

    oracle-pl-sql-programming-5th-edition

    Like its predecessors, this fifth edition of Oracle PL/SQL Programming covers language fundamentals, advanced coding techniques, and best practices for using Oracle’s powerful procedural language....

    CUDA Programming pdf

    The book is very much aimed at learning CUDA, but with a focus on performance, having first achieved correctness. Your level of skill and understanding of writing high-performance code, especially for...

    SQL Server 2016 Developer's Guide

    Compared to its predecessors, SQL Server 2016 offers developers a unique opportunity to leverage the advanced features and build applications that are robust, scalable, and easy to administer. ...

    MC74HC00A_datasheet-2.pdf

    This IC offers several advantages over its predecessors and is widely used in various digital electronic circuits for its robustness and compatibility. #### Key Features and Specifications - **...

    firefox setup stub 41.0.1

    The browser was built not to make a profit, but because its programmers thought that they could do better than its predecessors, Google and Microsoft. Firefox has an extremely active community of ...

    PhoneGap 3.x Mobile Application Development Hotshot.2014.pdf

    Version 3.x of PhoneGap introduced several enhancements and improvements over its predecessors, making it a more robust platform for mobile app development. Key features of PhoneGap 3.x include: - *...

    GMAT Mini-Test Questions and Answers

    The GMAT (Graduate Management Admission Test) is a standardized test used by many business schools to assess the readiness of candidates for advanced study in business and management. One of the ...

    professional sharepoint 2013 development

    SharePoint 2013, just like its predecessors, is a big product and this book is a big, diverse book. So, before you dive into the book, here’s a little about what each chapter entails and what you can...

    Microsoft Windows Server 2003 Administrators Pocket Consultant(2003

    As you'll see, Windows Server 2003 is very different from its predecessors and that meant a lot of research to ensure the book was as accurate as it could be. When all was said and done, I ended up ...

    EJB3 in action ORALCE PPT

    EJB3 introduces numerous improvements over its predecessors, focusing on simplicity and ease of use. By adopting a POJO-centric model and leveraging annotations for configuration, EJB3 significantly ...

Global site tag (gtag.js) - Google Analytics