Topic: Using a class before its full definition
Author: jamshid@ses.com (Jamshid Afshar)
Date: 1995/04/26 Raw View
Can a class which has only been declared be used in a class member
function declaration? ARM 7.1.1 says "The name of an undefined class
can be used in an 'extern' declaration.":
struct S;
extern S f(); // okay
Wouldn't it be better to say "... can be used in a declaration with
external linkage"?
struct S;
S f(); // no "extern", but same thing, right?
Also, allowing a undefined class to be used with any declaration with
external linkage would allow their use with member functions:
class SPString {
public:
class substring;
substring f(); // legal, right?
class substring {};
};
The case where I'm in disagreement with a compiler vendor is:
class SPString {
public:
class substring;
substring f() { substring s; return s; } // legal?
//...
class substring {};
};
I think this should be legal but IBM's xlC complains that "substring"
is undefined at the commented line. Other compilers I tested accept
it, but of course that doesn't mean it's legal.
Is the above code not legal because SPString::f() does not have
external linkage (inlines have static linkage), or because "substring"
is used in the body of f() before its definition? If the latter, that
sure seems silly because inline functions can refer to members
declared lexically after it and must be parsed as if they were
rewritten immediately after the class definition (see ARM 9.3.2).
Has ANSI/ISO looked into clarifying or expanding ARM 7.1.1? The
current rules seems to lead to absurdities like the above being
illegal but the following being legal:
class SPString {
public:
class substring;
substring f(); // legal (unless "inline" used here!)
class substring {};
};
// legal (unless the "extern only" rule is retroactive!)
inline SPString::substring f()
{ substring s; return s; }
Thanks,
Jamshid Afshar
jamshid@ses.com