开发者

Why can't I declare a struct variable inside a lambda in c++0x?

开发者 https://www.devze.com 2023-03-30 12:10 出处:网络
Here\'s the code. #include<struct.h> #include<iostream> #include<functional> using namespace std;

Here's the code.

#include<struct.h>
#include<iostream>
#include<functional>
using namespace std;

void LambdaTest(const function <struct dummy (void)>& f)
    {
    struct dummy test = f();
    cout<<test.a<<endl;
    }

int main()
    {
    int val = 5;
    struct dummy dum;

    auto func = [val](void) -> struct dummy
                        {
                        dummy temp;
                        temp.a = val;
                        return temp;
                        };

    LambdaTest(func);
    return 0;
    }

The file struct.h is very simple.

struct dummy
    {
    int a;
    };

GCC complains that

lambda_struct.cpp:19:38: error: field ‘temp’ has incomplete type

Is this allowed? If yes, then how do I fix it? If not, then why not?

EDIT:

The return type bug in the code (discovered by others) has now been fixed.

SOLUTION:

The problem is that C++0x standard allows definition to a new struct (and a class too, presumably) in the return type of a lambda definition itself. So if struct keyword is present in the return type, the compiler will t开发者_如何转开发hink that it is a new type and begin to complain.

The fixed code is

#include<struct.h>
#include<iostream>
#include<functional>
using namespace std;

void LambdaTest(const function <struct dummy (void)>& f)
    {
    struct dummy test = f();
    cout<<test.a<<endl;
    }

int main()
    {
    int val = 5;
    struct dummy dum;

    auto func = [val](void) -> dummy
                        {
                        dummy temp;
                        temp.a = val;
                        return temp;
                        };

    LambdaTest(func);
    return 0;
    }


The problem is that GCC incorrectly thinks you're declaring a new struct type on the trailing return, and it declares a field of an incomplete type that is the same type GCC thinks you're declaring.

It also complains that

error: 'temp' does not name a type

on the line with the assignment, because it is expecting a member declaration, not a statement.

Changing to:

auto func = [val](void) -> dummy
                        {
                            struct dummy temp;
                            temp.a = val;
                            return temp;
                        };

Will work.

Also, beware that not returning a value from a function will probably lead you into the realm of undefined behaviour.


What happens if you take away the struct part and just define the variable normally? You know, just:

dummy temp;

Also, try getting rid of the other superfluous struct in the lambda return value. Speaking of which, you need to actually return temp for it to compile.


In C++, unlike C, structures are not placed in a separate namespace, so you don't have to use the keyword struct in every declaration. Also, there is an error in your code, the lambda needs to return an instance of type dummy. The following compiles and runs as expected

#include<iostream>
#include<functional>
using namespace std;

struct dummy
    {
    int a;
    };

void LambdaTest(const function <dummy (void)>& f)
    {
    dummy test = f();
    cout<<test.a<<endl;
    }

int main()
    {
    int val = 5;
    dummy dum;

    auto func = [val](void) -> dummy
                        {
                        dummy temp;
                        temp.a = val;
                        return temp; // return the temp struct
                        };

    LambdaTest(func);
    return 0;
    }

Output:

5

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号