non-local function...uses anonymous type

Nov 26, 2009 at 2:22pm
I have the following code:
1
2
3
4
5
6
7
8
9
10
#include<cstdio>
typedef struct{
  int  exp;
}MyType[10];
void func(MyType a){
  return;
}
int main(){
  return(0);
}

When I use g++ to compile it, it said:
test.cpp:5: error: non-local function ‘void func(<anonymous struct>*)’ uses anonymous type

Add 'static' to the function it'll be fine. But anyone can tell me why?
Nov 26, 2009 at 2:57pm
You haven't named your struct, so MyType is an array of 10 elements of an anonymous struct that has an int named exp.

C treats structs differently. you'll find this code compiles in C, but not C++.
Nov 26, 2009 at 3:05pm
But if I define MyType as an anonymous struct (not an array), it'll be fine. And if I use static, no error either. So can you explain that why C++ treats this as error. In other words, why can't a non-local function use a pointer of anonymous type as parameter?
Thanks.
Nov 26, 2009 at 3:43pm
It doesn't complain using Microsoft VS2005. It does using gcc 4.3.2. I see no reason why it should complain myself.

Here are some links that discuss the matter. It looks like a GCC specific issue.
http://gcc.gnu.org/ml/gcc-bugs/2000-05/msg00909.html
http://archives.devshed.com/forums/development-94/gcc-4-0-non-local-variable-uses-anonymous-type-warning-129952.html
Nov 27, 2009 at 5:26am
The links help a lot. Thank you.
Topic archived. No new replies allowed.