Trouble getting lambda to work correctly

How might this be fixed to get the lambda to work?
Trying to evaluate if the string rating contains characters other than '*'

Thanks

1
2
3
4
5
6
std::string rating = "***x*";

if (rating.length() > 5) { throw TooLong(); }
else if (rating.length() <= 0) { throw TooShort(); }
else if ([](const std::string& rating) { return rating.find.first_not_of("*") == rating.npos; }) { throw InvalidCharacters(); }
else { _rating = rating; }
Last edited on
You have spelled find_first_not_of incorrectly.
GCC anyway gives a warning:
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
75
76
#include <exception>
#include <iostream>
#include <string>


class TooLong : public std::length_error {
public:
    TooLong();
};


TooLong::TooLong()
    : length_error("string is too long")
{
}


class TooShort : public std::length_error {
public:
    TooShort();
};


TooShort::TooShort()
    : length_error("string is too short")
{
}


class InvalidCharacters : public std::invalid_argument {
public:
    InvalidCharacters();
};


InvalidCharacters::InvalidCharacters()
    : std::invalid_argument("the only admited character is '*'")
{
}


bool checkString(const std::string & s_arg);


int main ()
{
    std::string rating { "***x*" };
    try {
        checkString(rating);
        std::cout << "Strng is fine.\n";
    } catch(std::exception & e) {
        std::cout << e.what() << '\n';
    }
    return 0;
}


bool checkString(const std::string & s_arg)
{
    if (s_arg.length() > 5) {
        throw TooLong();
    }
    
    if (s_arg.length() == 0) {
        throw TooShort();
    }

    if ( [](const std::string & s_arg) {
            return s_arg.find_first_not_of('*') == std::string::npos;
            } )
    {
        throw InvalidCharacters();
    }

    return true;
}


Compilation warning:
warning:
the address of 'static bool checkString(const string&)::<lambda(const string&)>::_FUN(const string&)'
will never be NULL [-Waddress]


Anyway, I can’t see the point of putting such an effort in that.
What’s bad in this?

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
#include <exception>
#include <iostream>
#include <string>


class TooLong : public std::length_error {
public:
    TooLong();
};


TooLong::TooLong()
    : length_error("string is too long")
{
}


class TooShort : public std::length_error {
public:
    TooShort();
};


TooShort::TooShort()
    : length_error("string is too short")
{
}


class InvalidCharacters : public std::invalid_argument {
public:
    InvalidCharacters();
};


InvalidCharacters::InvalidCharacters()
    : std::invalid_argument("the only admited character is '*'")
{
}


bool checkString(const std::string & s_arg);


int main ()
{
    std::string rating { "***x*" };
    try {
        checkString(rating);
        std::cout << "Strng is fine.\n";
    } catch(std::exception & e) {
        std::cout << e.what() << '\n';
    }
    return 0;
}


bool checkString(const std::string & s_arg)
{
    if (s_arg.length() > 5) {
        throw TooLong();
    }
    
    if (s_arg.length() == 0) {
        throw TooShort();
    }

    if ( s_arg.find_first_not_of('*') != std::string::npos ) {
        throw InvalidCharacters();
    }

    return true;
}


* * *
Please, let me hijack this thread and ask the good programmers here why gcc warns me about the address of that lambda.
To what I can see, it seems it wants me to be fully aware of the fact that a reference address (in this case, I guess of the parameter) would never be NULL. Very kind of it, but why in this case is so important?
Thanks for every explanation.
javascripty wrote:
How might this be fixed to get the lambda to work?

You need to actually invoke the lambda.

Something like this should work (passing rating as argument to the lambda).
 
if ([](const std::string& rating) { return rating.find_first_not_of("*") != rating.npos; }(rating))


javascripty wrote:
Trying to evaluate if the string rating contains characters other than '*'

You don't need to use a lambda to do that.

Something like this should work (and is less verbose).
 
if (rating.find_first_not_of("*") != rating.npos)


Enoizat wrote:
why gcc warns me about the address of that lambda. To what I can see, it seems it wants me to be fully aware of the fact that a reference address (in this case, I guess of the parameter) would never be NULL. Very kind of it, but why in this case is so important?

You get the same type of error if you put a function inside an if condition without calling it.
 
if (some_function)

This code is useless because the if statement is checking if the function pointer is not null, which is always true, because it points to some_function. If you write such code it is most likely a mistake.

This is probably what the author intended.
 
if (some_function())

Same problem with the lambda. The lambda was implicitly converted to a function pointer which is never null.
Last edited on
Peter87, thank you a lot.
I really asked a stupid question and wrote a flawed code. Changing the ‘if’ statement this way
1
2
3
if ( [&s_arg]() {
        return s_arg.find_first_not_of('*') != std::string::npos;
        }() )

looks like it solved the issue.

Great explanation, thanks again.
Topic archived. No new replies allowed.