C++ 11 Generic Lambda

Error: parameter declared auto
Error: Months was not declared in this scope

I can not figure out why I'm getting these error messages here. IS my complier to old GCC 4.7?



map<int, int> monthlengths{ {1,31}, {2,28}, {3,31}, {4,31},{5,31},{6,30}, {7,31},
{8,31}, {9,30}, {10,31}, {11,30}, {12, 31}};

int longmonths = count_if(begin(monthlengths), end(monthlengths), [](auto months){return months.second > 30;});
cout << longmonths << endl;

return 0;
Last edited on
What value do you use in the -std= option?
I'm trying to make a generic lambda work using c++ 11 compiler. I believe I have the correct type parameter as it will compile if I get ride of the greater than >. I don't understand that it wants an overload for the greater than. With it not being a class i'm not sure how I would do it?


map<int, int> monthlengths{ {1,31}, {2,28}, {3,31}, {4,31},{5,31},{6,30}, {7,31}, {8,31}, {9,30}, {10,31}, {11,30}, {12, 31}};

const int month = 30;
auto largeMonths = count_if(begin(monthlengths), end(monthlengths),[month]( decltype(*begin(monthlengths))& element)-> bool {return element > month; });

cout << largeMonths;
Generic lambdas were introduced in C++14.
http://en.cppreference.com/w/cpp/language/lambda
With it not being a class i'm not sure how I would do it?
int longmonths = std::count_if(std::begin(monthlengths), std::end(monthlengths), [](const std::pair<int, int>& months){return months.second > 30;});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <map>
#include <algorithm>

int main()
{
    const std::map<int, int> monthlengths{ {1,31}, {2,28}, {3,31}, {4,31},{5,31},{6,30},
                                           {7,31}, {8,31}, {9,30}, {10,31}, {11,30}, {12,31} };
    const int length = 30 ;

    {
        std::size_t cnt = 0 ;
        for( const auto& pair : monthlengths ) if( pair.second > length ) ++cnt ;
        std::cout << cnt << '\n' ;
    }

    {
        #if __cplusplus >=  201402L // C++14 or later
            std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                        [length] ( const auto& pair ) { return pair.second > length ; } ) << '\n' ;
        #else
            std::clog << "this compiler does not conform to C++14\n" ;
        #endif // __cplusplus
    }

    {
         using const_reference = decltype(monthlengths)::const_reference ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const_reference pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using const_reference = std::map<int,int>::const_reference ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const_reference pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = decltype(monthlengths)::value_type ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const value_type& pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = std::map<int,int>::value_type ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const value_type& pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = decltype( *monthlengths.cbegin() ) ; // const_reference
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( value_type pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = std::pair< const std::map<int,int>::key_type, std::map<int,int>::mapped_type > ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const value_type& pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = std::pair< const int, int > ;
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( const value_type& pair ) { return pair.second > length ; } ) << '\n' ;
    }

    {
         using value_type = std::pair<int,int> ; // *** if we are going to do this, we should pass by value
                                                 //     (semantically, a copy of the pair has to be made anyway)
         std::cout << std::count_if( std::begin(monthlengths), std::end(monthlengths),
                                     [length] ( value_type pair ) { return pair.second > length ; } ) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/7515523e7f44de2a
Topic archived. No new replies allowed.