Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Lambda expression parameter by Reference for C++0x

I am using a C++0x lambda expression to modify values of a map.

However, having difficulty passing the map iterator by reference.

If I just pass the iterator, by value such as: [](std::pair<TCHAR, int > iter) it compiles fine, but the values does not get updated in the map.

If I try to pass the iterator by reference, such as [](std::pair<TCHAR, int >& iter) the VS2010 compiler complains that it

cannot convert paramater from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &'

Here is the code. Appreciate information on how the std::map objects can be modified using the lambda expressions.

#include <tchar.h>
#include <map>
#include <algorithm>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::map<TCHAR, int > Map;

    Map charToInt;

    charToInt[_T('a')] = 'a';
    charToInt[_T('b')] = 'b';
    charToInt[_T('c')] = 'c';
    charToInt[_T('d')] = 'd';

    std::for_each(charToInt.begin(), charToInt.end(), [](std::pair<TCHAR, int >& iter)
    {
        int& val = iter.second;
        val++;
    });

    return 0;
}

Thank you

like image 806
Gopalakrishna Palem Avatar asked Oct 05 '10 07:10

Gopalakrishna Palem


People also ask

How to pass lambda functions in C++?

How To Pass Lambda Functions in C++ (By Value, By L-Value Reference, By Universal Reference) 11 minute read A question that I’ve asked on StackOverflow: C++ best practice: Pass use-only (not stored) lambda argument to function by const-reference or by forwarding-reference (a.k.a. universal-reference)tempted me to create this post.

What are lambda expressions in Java 8?

Lambda Expressions are anonymous functions. These functions do not need a name or a class to be used. Lambda expressions are added in Java 8. Lambda expressions basically express instances of functional interfaces An interface with a single abstract method is called a functional interface. One example is java.lang.Runnable.

Why can't I use empty parentheses in lambda expressions?

Because a parameter list is optional, you can omit the empty parentheses if you don't pass arguments to the lambda expression and its lambda-declarator doesn't contain exception-specification, trailing-return-type, or mutable. Typically, a lambda's function call operator is const-by-value, but use of the mutable keyword cancels this out.

How do you use Lambda in Python?

A lambda begins with the capture clause (lambda-introducer in the Standard syntax), which specifies which variables are captured, and whether the capture is by value or by reference. Variables that have the ampersand (&) prefix are accessed by reference and variables that do not have it are accessed by value.


1 Answers

The problem is that you are not allowed to modify the key of the map.

std::for_each(charToInt.begin(), charToInt.end(), [](std::pair<const TCHAR, int>& iter)

Will work because it uses const TCHAR.

Edit:

As @David and the other posters have pointed out, you would be better off using Map::value_type& which is a typedef for std::pair<const TCHAR, int>& in this case, because if you later change the types in the map you are using you won't need to change the loop code aswell.

For reference, here is the full error message, where you can see it is trying to convert between two different types of pair, one with TCHAR, the other with const TCHAR...

cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &'
    with
    [
        _Ty1=TCHAR,
        _Ty2=int
    ]
    and
    [
        _Ty1=const TCHAR,
        _Ty2=int
    ]
    and
    [
        _Ty1=TCHAR,
        _Ty2=int
    ]
like image 88
ngoozeff Avatar answered Sep 28 '22 09:09

ngoozeff