Discussion:
bds2006 cpp invalid (?) code accepted
(too old to reply)
dhoke
2008-05-27 17:26:23 UTC
Permalink
Somebody may want to try this with 2007 and see if its accepted, and report
it if:
1)I'm right that it shouldn't be accepted
2) nobody else has reported it

I don't believe the first private constructor prototype should be legal c++
(parameter "const &somecls"). The second/third (commented) prototypes were
what I wanted, but didn't discover the mistake until after the 15-20 minute
build wait (change at low-level header) and subsequent code exercise (to
find it still wasn't working correctly), and then wondering why my "private"
constructor hadn't caused errant code (attempting to pass class
inappropriately by value) to be caught.

(I know there are other situations I've encountered where "int" was
apparently defaulted as return type on methods, and I guess this is probably
another manifestation of that behaviour.)
===========
#include <stdio.h>

class somecls
{
public:
somecls() { } ;
private:
somecls(const &somecls) { } ;
// somecls(const somecls &somecls) { } ;
// somecls(const somecls &) { } ;
} ;

void otherfunc(somecls acls)
{
//compiler doesn't complain about missing type on first private
constructor!!!
printf("Why can't the compiler get it right?\n") ;
}

int main(int argc, char *argv[])
{
somecls acls ;

otherfunc(acls) ;

return 0 ;
}
Ed Mulroy [TeamB]
2008-05-27 18:27:03 UTC
Permalink
Post by dhoke
I don't believe the first private constructor prototype should be legal
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
I do not understand why that would be invalid. The private constructor
takes a constant reference to an instance of the class. There is no name
for the calling argument but that is the normal way one shows unused
parameters in C++. The public somecls() constructor is used in the
displayed code but the private constructor is not used even in the place
where your comment says it is.

. Ed
Post by dhoke
Somebody may want to try this with 2007 and see if its accepted, and
1)I'm right that it shouldn't be accepted
2) nobody else has reported it
I don't believe the first private constructor prototype should be legal
c++ (parameter "const &somecls"). The second/third (commented) prototypes
were what I wanted, but didn't discover the mistake until after the 15-20
minute build wait (change at low-level header) and subsequent code
exercise (to find it still wasn't working correctly), and then wondering
why my "private" constructor hadn't caused errant code (attempting to pass
class inappropriately by value) to be caught.
(I know there are other situations I've encountered where "int" was
apparently defaulted as return type on methods, and I guess this is
probably another manifestation of that behaviour.)
===========
#include <stdio.h>
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
// somecls(const somecls &somecls) { } ;
// somecls(const somecls &) { } ;
} ;
void otherfunc(somecls acls)
{
//compiler doesn't complain about missing type on first private
constructor!!!
printf("Why can't the compiler get it right?\n") ;
}
int main(int argc, char *argv[])
{
somecls acls ;
otherfunc(acls) ;
return 0 ;
}
dhoke
2008-05-27 18:42:19 UTC
Permalink
Post by Ed Mulroy [TeamB]
Post by dhoke
I don't believe the first private constructor prototype should be legal
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
I do not understand why that would be invalid. The private constructor
takes a constant reference to an instance of the class. There is no name
for the calling argument but that is the normal way one shows unused
parameters in C++. The public somecls() constructor is used in the
There actually is a name for the argument (confusingly, same as the class
name) - but not a type...

What is the type of the argument?

(I failed to specify it... expect it has defaulted to "int", as have seen
done on methods in past when I incorrectly omitted a return type definition
on method prototype.)
Post by Ed Mulroy [TeamB]
displayed code but the private constructor is not used even in the place
where your comment says it is.
. Ed
Alan Bellingham
2008-05-27 18:59:14 UTC
Permalink
Post by Ed Mulroy [TeamB]
Post by dhoke
I don't believe the first private constructor prototype should be legal
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
I do not understand why that would be invalid. The private constructor
takes a constant reference to an instance of the class. There is no name
for the calling argument but that is the normal way one shows unused
parameters in C++.
No, Ed, you've been misled. The line in question will almost certainly
still compile if it read "somecls(const &EdMultoy) { } ;" instead. What
is actually happening is an old, old rule is being invoked by the
compiler. Try the following:

struct somecls
{
somecls(const &somecls) { } ;
somecls(const int& somecls) { } ;
};

You'll get a duplicate definition error. In other words, "const
&somecls" is being interpreted exactly as "const int& somecls", and far
from being unnamed, the parameter is actually a named reference to a
const int.

It goes back to the days when int was assumed. You could type any of the
following

unsigned x = 0;
long y = 0;
const z = 0;

and all would compile, since in all cases, at least some of the type
declaration had been given.

These days, you can still miss the 'int' from the first two, (and almost
everyone does from the second), but not from the last. And the
declaration should be invalid.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Ed Mulroy [TeamB]
2008-05-27 21:55:24 UTC
Permalink
That's what I get for reading questions and answering quickly - my "ready,
shoot, aim" style :-(

Because he used the same name as the class name for the calling argument I
totally missed that it was
const & somecls
and not
const somecls &

Good catch on my goof - thanks!

FWIW:
The default of making 'int' the type when a type spec is missing is an
old-timey C thing discouraged since the prelim Ansi C spec was released
around '86. I don't think it ever applied in C++ but most C++ compilers do
or at least did honor it. I dislike code that depends upon that.

. Ed
Post by Alan Bellingham
Post by Ed Mulroy [TeamB]
Post by dhoke
I don't believe the first private constructor prototype should be legal
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
I do not understand why that would be invalid. The private constructor
takes a constant reference to an instance of the class. There is no name
for the calling argument but that is the normal way one shows unused
parameters in C++.
No, Ed, you've been misled. The line in question will almost certainly
still compile if it read "somecls(const &EdMultoy) { } ;" instead. What
is actually happening is an old, old rule is being invoked by the
struct somecls
{
somecls(const &somecls) { } ;
somecls(const int& somecls) { } ;
};
You'll get a duplicate definition error. In other words, "const
&somecls" is being interpreted exactly as "const int& somecls", and far
from being unnamed, the parameter is actually a named reference to a
const int.
It goes back to the days when int was assumed. You could type any of the
following
unsigned x = 0;
long y = 0;
const z = 0;
and all would compile, since in all cases, at least some of the type
declaration had been given.
These days, you can still miss the 'int' from the first two, (and almost
everyone does from the second), but not from the last. And the
declaration should be invalid.
Sergiy Kanilo
2008-05-27 18:55:05 UTC
Permalink
Post by dhoke
Somebody may want to try this with 2007 and see if its accepted, and report
no error or warnings when compiled with CG2007
Post by dhoke
1)I'm right that it shouldn't be accepted
I believe so
Post by dhoke
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
[...]
Post by dhoke
int main(int argc, char *argv[])
{
somecls acls ;
when I tried using

somecls acls( 0 );

I've got

E2247 'somecls::somecls(const int &)' is not accessible

so it seems that compiler treats

somecls(const &somecls) { }

as

somecls(int const &) { }

Cheers,
Serge
Duane Hebert
2008-05-27 19:31:06 UTC
Permalink
Post by Sergiy Kanilo
Post by dhoke
Somebody may want to try this with 2007 and see if its accepted, and
no error or warnings when compiled with CG2007
Post by dhoke
1)I'm right that it shouldn't be accepted
I believe so
Post by dhoke
class somecls
{
somecls() { } ;
somecls(const &somecls) { } ;
[...]
Post by dhoke
int main(int argc, char *argv[])
{
somecls acls ;
when I tried using
somecls acls( 0 );
I've got
E2247 'somecls::somecls(const int &)' is not accessible
so it seems that compiler treats
somecls(const &somecls) { }
as
somecls(int const &) { }
VS 2005 gives me this:
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
Loading...