Re: Strange bug: struct is not a type in C++?
Re: Strange bug: struct is not a type in C++?
- Subject: Re: Strange bug: struct is not a type in C++?
- From: Jean-Denis Muys <email@hidden>
- Date: Thu, 09 Oct 2008 08:49:04 +0200
Thank you for all the answers. Emil got it right: the line must be
prepended by the "typename" keyword.
The reason is that there is no unambiguous way to tell what the
"class1<Type>::X *p2" is until class1 is instanciated. It can be two
very different things:
1- A pointer declaration, when X is a type inside class1. This is the
original intent.
2- a multiplication (!!), when X is (for example), an int member of
class1. This could happen.
This is why the C++ standard now says that an identifier that is both
qualified (using the :: scope resolution operator) AND dependent
(parameterized) is NOT automatically a type.
Prepending it with "typename" makes it a type:
typename class1<Type>::insideStruct *p2;
This also explains why prepending struct works as well. With struct,
the line says that X is a struct, and since a struct is automatically
a type in C++, the compiler is happy.
However the "struct" workaround is less general and not idiomatic.
I had not written any significant C++ code since 2003. Since then the C
++ standard has evolved. My opinion is that this is yet another
example why Objective-C land is much more comfortable. Hopefully I
won't have to drift back to C++ too often!
Thanks and best regards to all.
Jean-Denis
On 8 oct. 08, at 09:24, Jean-Denis Muys wrote:
Hi,
As a relative newcomer, I have a strange bug in my program, which I
managed to reduce to the tiny C++ code below (use a standard C++
tool as a project type in XCode).
The compiler reject the "class1<Type>::insideStruct *p2;"
declaration with the following error message:
/.../main.cpp:23: error: expected ';' before '*' token
while prepending "struct" in front of the declaration makes it quite
happy.
Since in C++, structs are types, this shouldn't be: either they are
both correct or they are both incorrect.
Did I miss something? Is this an XCode bug (I use version 3.1.1)? or
my bug?
Thanks for the help,
Jean-Denis.
#include <iostream>
template<class Type>
class class1
{
public:
struct insideStruct
{
int anyInt;
Type fData;
};
insideStruct class1Data;
};
template<class Type>
class class2
{
public:
int class2int;
struct class1<Type>::insideStruct *p1; // Compiler is just fine
with this
class1<Type>::insideStruct *p2; // but doesn't like this
};
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, Lucky World!\n";
class1<double> c1;
class2<double> c2;
std::cout << "class1 size: " << sizeof(c1) << "\n";
std::cout << "class2 size: " << sizeof(c2) << "\n";
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden