views:

26

answers:

2

check this out:

this compiles fine on iPhone:

typedef int ATYPE;

void AFunc()
{
    ATYPE ATYPE;
    ATYPE = 1337;
}

this compiles fine on iPhone:

typedef int ATYPE;
typedef ATYPE _ATYPE;

struct AStruct
{
    _ATYPE ATYPE;
};

void AFunc()
{
    AStruct bob;
    bob.ATYPE = 1337;
}

but this does NOT:

typedef int ATYPE;

struct AStruct
{
    ATYPE ATYPE;
};

void AFunc()
{
    AStruct bob;
    bob.ATYPE = 1337;
}

the above compiles fine on other platforms though.

I suppose we can work around it by doing that second example, but does anyone know why this is?

A: 

This is because the iPhone compiler does not correctly recognize that the code in the first case is invalid C++, but for some reason in the third case it does correctly recognize the error.

You can see that it's invalid to give types and variables the same name, because allowing it would lead to confusion:

typedef short atype;
long long atype;
printf("%d", sizeof(atype));  // What does this print?

It's fairly common for compilers to fail to emit errors when given invalid code, and instead to compile it as if it were correct. It just happens.

Brooks Moses
well it does work fine, and it prints the size of long longas long as the typedef is in a different scope (like global) than where it is declared as a variable, it seems to work fine, I suppose it acts just like how local variables override global variables or member variables of the same name.
matt
Hmm; fair point about scoping and things overriding things in more-global scopes. I still think it's ugly code that's exercising a deep corner case of the spec if it's permitted, but I can see how it might be.
Brooks Moses
+2  A: 

Well, if you don't like my previous answer, here's the alternate one. The online Comeau C++ compiler at http://www.comeaucomputing.com/tryitout/ compiles your third example without error. Given that that's typically considered a gold standard among C++ compilers, this suggests that this may well be a bug in the G++ compiler in the iPhone SDK (and, of course, in the other versions of G++ that I referenced in my comment).

If that's true -- and I don't have the C++ spec at hand to argue the fine details -- the answer to your "why?" question is, "Because G++ has a weird corner-case bug. Please file an issue in the GCC bug tracker about this, so that someone will fix it."

Brooks Moses