C++14 and Lambda__c++

Source: Internet
Author: User
Tags mul
about c++14According to Hurb Sutter, in a speech titled "The Future of C + +", the C + + Standards Committee, after introducing the C++11 standard, did not make too much of a break to put it into the next two generations of standards (C++14 and C++17). The c++14, which is expected to be launched next year, is set to revise and refine the current C++11 standard. Although the time is not comfortable, the next generation of standard c++14 still contains some interesting new features. According to the existing proposals, the biggest new feature of C++14 is the extension of lambda and common function functions. Here are some things that are likely to add to the new standard. automatic derivation of functions and types of lambda return valuesStatus: The type of the lambda return value can be inferred automatically by the compiler in two cases without explicit designation.
When there is no return statement in the function body of a lambda, the returned value type is void
[] () {std::cout << "Hello";};
Explicitly indicate the return value type as follows
[] ()->void{std::cout << "Hello";};

When a function body of a lambda contains only a return statement, the returned value type is equivalent to the type of the expression in the statement
[] (int x) {return x + 1;};
The return statement expression X + 1 is of type int, so the returned value type is int
//explicitly indicating the return value type as follows
[] (int x)->int{return x + 1;};
Flaw: In most cases, the compiler does not have the ability to automatically deduce a lambda and function return value type.
For a lambda, as long as there is a return value and the function body contains more than one statement,
//the type of its return value cannot be automatically inferred by the compiler, you must explicitly specify
[] (int x)->int{int y = x + 1; return y;};

For a normal function or function template, even if the function body contains only one statement
//The type of the return value cannot be automatically inferred by the compiler, you must explicitly specify
int f (int x) {return x + 1;}

For ordinary functions or function templates, even if the function takes a return value type and the
type of the return value is not automatically inferred by the compiler, you must explicitly specify
template<typename T, typename u>
Auto Mul (const t& T, const u& u)-> decltype (t * u)
{return
	t * u;
}
Countermeasures: In most cases the compiler will have the ability to automatically infer the lambda and function return value types.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
The type of the lambda return value can be inferred automatically by the compiler without explicitly specifying
[] (int x) {int y = x + 1; returns y;};

The type of the normal function return value simply specifies
that the Auto//compiler will automatically derive its return value of type
auto F (int x) {returns x + 1;}

The type of return value for a function template is only specified as the auto
//compiler will automatically derive its return value type
template<typename T, typename u>
Auto Mul (const t& T, const u& u)
{return
	t * u;
}
single-state lambda and polymorphic LambdaStatus quo: The lambda in the current standard is a single state (monomorphic) lambda, that is, the type of the lambda parameter must be explicitly specified and cannot be automatically inferred by the compiler through the context.
Std::for_each (v), End (v), [] (Decltype (*begin (v)) x) {std::cout << x;});
The for_each algorithm function is used to traverse
the set//the algorithm requires that the third parameter is a function or function object that receives a collection element as a unique parameter.
So the type of the unique parameter x of the lambda itself as the third parameter of the algorithm function can only be the element type of the set V
//Because the compiler does not automatically derive the function of the lambda parameter type according to the context///the type of the
element x of the set v must be given explicitly: Decltype (*begin (v))
Flaw: There is no polymorphic (polymorphic) lambda or generic (generic) lambda in the current standard. This means that the missing parameter types in the language can be automatically deduced by the compiler based on the context of the lambda.
Try to compare C # code similar to the following features:
Lst. ForEach (x => {Console.WriteLine ("{0}", x);});
Here List<t> 's foreach method is used to traverse the list
//The method requires a delegate that can receive the collection element type as an argument
// Therefore, the type of the unique parameter x of the lambda itself as the parameter of this method can only be the element type of the collection LST
//Because the C # compiler has the ability to automatically derive the lambda parameter type from the context//
There is no need to give the type of element x of LST
Countermeasures: Add a new feature of the generic (generic) lambda (a lambda that can be automatically inferred by the compiler based on the context).
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
At present, the grammatical form of generic lambda is controversial
//grammatical form one (the initial proposal, the standard committee discussion is more controversial, difficult to pass)
Std::for_each (v), End (v), [] (x) {Std::cout << x; } );
Syntax form two (feasible scheme)
Std::for_each (Begin (V), End (v), [] (&x) {std::cout << x;});
Syntactic form III (feasible scheme)
Std::for_each (Begin (V), End (v), []<> (&x) {std::cout << x;});
Syntax Form IV (improved proposal, more controversial in C + + discussion group, but relatively easy to pass)
Std::for_each (Begin (V), End (v), [] (auto& x) {std::cout << x;});
lambda with template parametersStatus and drawbacks: No lambda with template parameters exists in the current standard.
Unable to overwrite the following function template with the equivalent lambda
template<typename T, TypeName u>
Auto Mul (const t& T, const u& U)-> D Ecltype (T * u)
{return
	t * u;
}
Countermeasures: Add a new feature of lambda with template parameters.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
Lambda Auto mul with template parameters
= []<typename T, TypeName u> (const t& T, const u& U) {return t * u;}
the function body is the lambda of an expressionStatus quo: The function body of a lambda has only one form, even in the form of a large bracket block.
Even the simplest lambda (the function body contains only a return statement for returning expressions), the function body must also take the form of a block of code
auto F = [] (int x) {returns x + 1;};

Nested lambda
[] (int x)->function<function<int (int) > (int) >{return
	[=] (int y)->function <int (int) >{return
		[=] (int z) {return
			x + y + z;
		}
;};
Flaw: The simplest lambda writing is less concise than the C # language.
Try to compare C # code similar to the following features:
C # allows the function body of a lambda to take the form of an expression.
Func<int, int> = x => x + 1;

Nested lambda
func<int, Func<int, Func<int, int>>> g = x => y => z => x + y + z;
Countermeasures: Allow the simplest lambda function body to take the form of an expression.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
Allow the function body of the lambda to take the form of an expression of
auto F = [] (int x) x + 1;

Nested lambda
[] (int x)->function<function<int (int) > (int) >
	[=] (int y)->function<int ( int) >
		[=] (int z) x + y + z;
Countermeasures: Given the consistency of the language, normal functions and function templates, if converted to lambda syntax, the function body will also be allowed to take the form of expressions.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
Use the extended lambda syntax
//function template below
Template<typename T, TypeName u>
Auto Mul (const t& T, const u& U)-& Gt Decltype (T * u)
{return
	t * u;
}
Can be rewritten as
Template<typename T, TypeName u>
[] mul (const t& T, const u& u) T * u;
capture expression of a lambdaStatus quo: By reference capture, value capture, and so on, the function body of a lambda can capture and use local variables defined in the periphery of the lambda, as well as class member variables.
Reference capture
int n = 1;
[N] () {
    n++;//n==2
} ();
n==2

//value capture
int n = 1;
[=] () mutable{
    n++;//n==2
} ();
N==1
Flaw: Capture is still insufficient, such as mobile capture can not be achieved.
There is no moving capture, only passing
std::vector
Countermeasures: By introducing the capture expression, the common capturing mode is realized.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
Implementation of mobile capture std::vector
correlation extension of lambda parametersStatus and defects: The function signature of a lambda has no default parameter and cannot omit the parameter name compared with the normal function.
The following function cannot be rewritten as an equivalent lambda
int f (int x = 0) {return x + 1;}
void g (INT/* unused */) {std::cout << "G";}
Action: Allows the function signature of a lambda to have default parameters, allowing omitting the parameter names.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
Default parameters and omitted parameter names
auto f = [] (int x = 0) {return x + 1;};
Auto g = [] (int/* unused */) {std::cout << "G";};
named LambdaStatus and defects: Lambda has no name, cannot overload, and recursion.
Lambda can be crowned, but cannot overload
auto F1 = [] (int x) {std::cout << "f int";};
Auto F2 = [] (double x) {std::cout << "F Double";};

Lambda cannot be directly recursive
//auto factorial = [] (int x)->int{return x = = 0? 1:x * factorial (x-1);
Std::function<int (int) > factorial = [ampersand] (int x) {return x = = 0? 1:x * factorial (x-1);
Countermeasures: Add a new feature named Lambda.
Note: The following C + + code contains the expected language new features that are not currently supported by the compiler.
A named Lambda can overload
[]f (int x) {std::cout << "f int";};
[]f (Double x) {std::cout << "F Double";};

A named Lambda can also be recursive
[]factorial (int x)->int{return x = = 0 1:x * factorial (x-1);
RELATED LINKS Generic (polymorphic) Lambda http://cpp-next.com/files/2012/09/N3418.pdf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.