- 浏览: 387636 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15277首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 958RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 742It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 913As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1029typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 732There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 778we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 905As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 715There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 925Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 916As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 946As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1282in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 895class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 952template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1175There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 789the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 546The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1126In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 832A class member operator new() 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 ...
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 ...
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 ...
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....
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 ...
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++ ...
and much more <br>Windows Vista is a vast improvement over its predecessors, with an appealing, glass-like visual overhaul, superior searching and organization tools, a multimedia and ...
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....
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...
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....
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...
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. ...
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 ...
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 ...
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 ...
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...
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 ...
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 ...