`

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.

 

分享到:
评论

相关推荐

    C++程序设计语言(特别版) 英文原版

    than its predecessors were.The explosion of C++use and the massive amount of experience accu- mulated as a result makes this possible. The definition of an extensive standard library makes a ...

    C++程序设计语言特别版

    Standard C++ is a far more powerful and polished language than the version of C++ introduced by the first edition of this book. New language features such as namespaces, exceptions, templates, and ...

    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 ...

    Time_Series_Analysis_and_its_Applications_with_R_Examples_4th_ed

    The fourth edition of this popular graduate textbook, like its predecessors, presents a balanced and comprehensive treatment of both time and frequency domain methods with accompanying theory....

    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...

    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....

    Oracle PL/SQL programming(5th Edition)

    for example, he explores Oracle’s different ways of formatting numbers, then details the behavior of the to_number function under different conditions (with and without a specified format model, and...

    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. ...

    Computer Architecture A Quantitative Approach 4th Edition

    as its predecessors, this edition aims to demystify computer architecture through an emphasis on cost-performance-power trade-offs and good engineering design. We believe that the field has continued ...

    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 ...

    Windows 7 Administrator’s Pocket Consultant.pdf

    ever, is just how different Windows 7 is from its predecessors—and that’s because many of the most significant changes to the operating system are below the surface. These changes affect 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 ...

    CRC.Project.Management.Maturity.Model.3rd.Edition.1482255448.pdf

    Following in the tradition of its bestselling predecessors, Project Management Maturity Model, Third Edition provides a roadmap for improving project success and boosting organizational performance....

    控制系统设计指导

    Control systems are broadly used and are thus well integrated into our educational system.Courses are offered at most engineering universities, and a few disciplines even require students to undergo ...

Global site tag (gtag.js) - Google Analytics