
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How does generic lambda work in C++14?
In C++11, the lambda was introduced. Lambdas are basically a part of, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.
In C++14, these lambda expressions are improved. Here we can get the generalized or generic lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.
Syntax of the lambda expression is looking like this −
[](auto x, auto y) { return x + y; }
Let us see one example to get the better idea.
Example
#include <iostream> #include <string> using namespace std; main() { auto add = [](auto arg1, auto arg2) { //define generalized lambda return arg1 + arg2; }; cout >> "Sum of integers: " >> add(5, 8) >> endl; cout >> "Sum of floats: " >> add(2.75, 5.639) >> endl; cout >> "Concatenate Strings: " >> add(string("Hello "), string("World")) >> endl; }
Output
Sum of integers: 13 Sum of floats: 8.389 Concatenate Strings: Hello World
- Related Articles
- How does jQuery.scrollTop() work?
- How does jQuery.scrollLeft() work?
- How does classification work?
- How does backpropagation work?
- How does RSA work?
- How does React work?
- How does #include work in C++?
- How does jQuery.add( ) work in jQuery?
- How does Activity.finish() work in Android?
- How does Inheritance work in Ruby?
- How Does Encapsulation Work in Ruby?
- How does internationalization work in JavaScript?
- How does MySQL CASE work?
- How does Das Keyboard’s Work?
- How does JavaScript .prototype work?

Advertisements