C ++ development and abstract

Prior to first say some concepts:

Computer two most important elements: CPU (controller, operator), storage devices (registers, cache, memory, hard disk).

CPU of the corresponding algorithm, the function program corresponding to the algorithm

Correspondence data storage device, the data corresponding to the program variables

Von Neumann's most brilliant minds is this: the algorithm recorded as instruction, as data storage, so the perfect combination of algorithms and data, resulting in a von Neumann architecture computers. This is similar to people, think carefully, the human brain also compute and storage capabilities.

Zatan:

Two years ago saw "hackers and painters" This book was very interesting that the second half of the book strongly advocated the Lisp language, said the future of the language lisp programming language, extremely abstract, can be very labor-saving to do other language is difficult to do or do not do.

Which gave an excellent example, he wrote a short lisp code argument is a function, the return value is a function. That is possible to function as data processing. As the code corresponding to the data processing.

This capability is very attractive, in C ++, the code is .text, data is .data, distinction is obvious. In lisp, the code data and no clear distinction.

Some have called lisp language-oriented languages, functional programming originator, metamorphosis of language, the language of non-normal use ......

lisp really attracted me, the authors say the most valuable is human, so the thing to do as much as possible to the program, lisp is such a language, the algorithm can be used as data, so it can in Artificial Intelligence field to display their fists.

When I try to learn lisp, I find all its code in brackets, the result of the expression (3 + 4) is 7, and the normal language is 3 + 4. So read lisp code that was struggling. Finally, I did not use lisp. But I was willing to call it functional programming originator.

In recent years, functional programming language is accepted by many, python to support functional. Today I want to say is I often use C ++ supports functional.

I think carefully, so why lisp flexibility to combine data and functions. Finally, I would come out, lisp and python are dynamic languages ​​to generate a command at runtime, so at runtime can be dynamically generated code. The C ++ language is static, put the instruction at compile dead. So if C ++ supports functional programming, it must be decided upon in the compiler.

Sure enough, the C language function pointer, to the C ++ function object, and then the C ++ lambda 11, functional, bind. The compiler are complete functional, and by meta-programming (macros, templates) to achieve.

Decorator function:

In "Hackers and Painters" in lisp author of the code posted, it is a function decorator, and takes a function, this function to modify, and then returns a new function. This function is called a function decorator. When he believes that C ++ is completely impossible to achieve this functionality, I tried many times, did achieve does not come out, but the recent discovery of a shadow function decorators in ceph code, although there is no simple lisp, but the function is achieved out: Here I'll just paste the code.

#include <utility>
class Decorator
{
public:
	template <typename Callback, typename...Args>
	decltype(auto) ret(Callback&& cb, Args&&... args)
	{
		//log
		printf("begin\n");
		printf("to do some things\n");
		auto ret = std::forward<Callback>(cb)(std::forward<Args>(args)...);
		//log
		printf("end\n");
		printf("to do some things\n");
		return ret;
	}
	template <typename Callback, typename...Args>
	void voi(Callback&& cb, Args&&... args)
	{
		//log
		printf("begin\n");
		printf("to do some things\n");
		std::forward<Callback>(cb)(std::forward<Args>(args)...);
		//log
		printf("end\n");
		printf("to do some things\n");
	}
};

int add(int a, int b) {  return a + b; }
void sub(int a, int b) {}
int main()
{
	Decorator dec;
	dec.ret(add, 1, 3);
	dec.voi(sub, 1, 3);
	system("pause");
	return 0;
}

  

Wherein, ret function template may be packaged with a function return values, voi function template may wrapper function returns no value. Free to experience, and then modify according to their needs.

Guess you like

Origin www.cnblogs.com/xjjsk/p/11692718.html