🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1006

Re: type definitions and scopes

#1006
From: David Michaels
Date: Thu, 04 Apr 1991 20:12
62 lines
3246 bytes
In <13998@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes:

-> I have tried unsuccessfully to find an unambiguous statement....
-> in the standard that allows or prohibits the following cases:
-> 	1) a function declaration that defines a new 
-> 	   type in the return type, and,
-> 	2) a function declaration that defines a new type in a parameter

In <13999@darkstar.ucsc.edu> daniel@terra.ucsc.edu (Daniel Edelson) writes:

-> Sorry, I meant to send this to comp.std.c...

Well, comp.std.c++ is also appropriate; I've been meaning to bring up this nit.

According to ANSI-C, defining a new type in a (1) return type,
(2) parameter, (3) cast expression, and (4) sizeof expression,
is legal (well, at least it doesn't say that any of these are illegal,
and the Plum-Hall validation suite explicitly tests for (2) and (4)).

But, in the C++ARM, defining a new type in a (1) return type,
(2) parameter, and (3) cast expression is explicitly prohibited
(sec.8.2.5, p.139 and sec.5.4, p.67).

The stated reason for (1) and (2) is that C++ relies on name equivalence,
and since the defined tag name would be in the scope of the function, the
*only* legal calls of the function would be from within the function itself.

This reasoning does not seem to apply at all to case (1); I don't see any
reason for this restriction whatsoever; it seems to be a fairly reasonable
thing to want to do, e.g.:

        struct S { int a, b; } f () { }
        main () { struct S x = f (); }

As for case (2), this just isn't true; consider:

        void f (struct S { int a, b; } *arg) { }
        main () { f (0); /* legal call to f with null pointer constant */ }

And as for case (3), the reasoning that the result would not be usable
is also not true; consider:

        main () { void *p = (struct T { int c, d; } *)0; }

Restrictions (2) and (3) are perfectly fine with me, but I think the real
reason for these restrictions is simply that it's bad programming practice,
not for any other technical reasons.  No?

Anyways, my question is, does the same kind of restriction apply to
defining a new type in a sizeof expression, and new expression.  E.g.

        int x = sizeof (struct S { int a, b; });  // legal ?
        void *p = new (struct T { int c, d; });   // legal ?

I can't find anything in the C++ARM which prohibits these.
Did I miss it?  Should I assume it's simply an oversight?  Thanks.

                                + -------------------------------------+
                                | David Michaels (david@lpi.liant.com) |
                                | Language Processors, Inc. (LPI)      |
                                | Framingham, MA (508) 626-0006        |
                                + -------------------------------------+
Technical Details
Message-ID: <1991Apr4.201228.986@lpi.liant.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!lpi.liant.com!david
References: <13998@darkstar.ucsc.edu> <13999@darkstar.ucsc.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!lpi.liant.com!david
From: david@lpi.liant.com (David Michaels)
Newsgroups: comp.std.c++
Subject: Re: type definitions and scopes
Message-ID: <1991Apr4.201228.986@lpi.liant.com>
Date: 4 Apr 91 20:12:28 GMT
References: <13998@darkstar.ucsc.edu> <13999@darkstar.ucsc.edu>
Organization: Language Processors Inc., Framingham MA
Lines: 62