Board index » cppbuilder » bds2006 cpp invalid (?) code accepted

bds2006 cpp invalid (?) code accepted


2008-05-28 01:26:23 AM
cppbuilder99
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 ;
}
 
 

Re:bds2006 cpp invalid (?) code accepted

Quote
I don't believe the first private constructor prototype should be legal
c++ (parameter "const &somecls"). ... :
class somecls
{
public:
somecls() { } ;
private:
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
Quote
dhoke wrote in message
news: XXXX@XXXXX.COM ...

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 ;
}
 

Re:bds2006 cpp invalid (?) code accepted

"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
>I don't believe the first private constructor prototype should be legal
>c++ (parameter "const &somecls"). ... :
>class somecls
>{
>public:
>somecls() { } ;
>private:
>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.)
Quote
displayed code but the private constructor is not used even in the place
where your comment says it is.

. Ed

 

{smallsort}

Re:bds2006 cpp invalid (?) code accepted

dhoke wrote:
Quote
Somebody may want to try this with 2007 and see if its accepted, and report
it if:
no error or warnings when compiled with CG2007
Quote
1)I'm right that it shouldn't be accepted
I believe so
Quote
class somecls
{
public:
somecls() { } ;
private:
somecls(const &somecls) { } ;
[...]
Quote
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
 

Re:bds2006 cpp invalid (?) code accepted

"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
>I don't believe the first private constructor prototype should be legal
>c++ (parameter "const &somecls"). ... :
>class somecls
>{
>public:
>somecls() { } ;
>private:
>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
 

Re:bds2006 cpp invalid (?) code accepted

"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
Quote
dhoke wrote:
>Somebody may want to try this with 2007 and see if its accepted, and
>report it if:

no error or warnings when compiled with CG2007

>1)I'm right that it shouldn't be accepted

I believe so

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

[...]

>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
 

Re:bds2006 cpp invalid (?) code accepted

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
Quote
Alan Bellingham wrote in message
news: XXXX@XXXXX.COM ...

>>I don't believe the first private constructor prototype should be legal
>>c++ (parameter "const &somecls"). ... :
>>class somecls
>>{
>>public:
>>somecls() { } ;
>>private:
>>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.