- 浏览: 399541 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 771It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 960As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A class member operator new() c ...
相关推荐
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 ...
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...
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. ...
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 - **...
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 ...
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: - *...
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 ...
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 ...
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 ...