Re: new foo::foo() erroneous, ugly, or OK?
Victor Bazarov wrote:
usenet@schweikhardt.net wrote:
gcc accepts the following code, while
FlexeLint 8.00u reports an error:
$ cat foo.cpp
class foo {
public:
foo() { }
};
int main (void)
{
foo *myfoo = new foo::foo();
delete myfoo;
return 0;
}
---snip---
FlexeLint for C/C++ (Unix) Vers. 8.00u, Copyright
Gimpel Software 1985-2006
--- Module: foo.cpp (C++)
_
foo *myfoo = new foo::foo();
foo.cpp 8 Error 1018: Expected a type after 'new'
[...]
I'm not a C++ expert, so my question to the resident AI is:
Is this in fact an error, or does the C++ standard
allow this syntactically/semantically?
It is perfectly fine. 'foo' _class_ owns the name 'foo' as its
own type. 'foo::foo' is just as good as a *type-id* as, for
example 'foo::foo::foo::foo::foo::foo'. There should be no error
report. FlexeLint is buggy (as much as they love touting their
own horn, thier software is written by people too).
No, the expression is illegal according to ?3.4.3.1. "The [interior]
name shall represent one or more members of that class or of one of its
base classes". Since "foo" does not name a member of "foo" or one of
its base classes, the expression is illegal and should not compile.
The expression is almost legal in the latest draft C++ standard - as
the name of a constructor. But as the following example shows, without
the "class" or "struct" keyword, the expression is still an error:
struct A { A(); };
struct B: public A { B(); };
...
A::A a; // error, A::A is not a type name
struct A::A a2; // object of type A
Even gcc acknowledges that prefixing the class name is not standard C++
- but allows it anyway as a harmless redundancy. Whether one shares
that view or not, there is no question that this syntax is errorneous
according to both the current and forthcoming C++ Standard.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]