C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fun with C++
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Hyman Rosen
Guest





PostPosted: Sun Dec 05, 2004 11:16 am    Post subject: Fun with C++ Reply with quote



This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.
The following program is legal and compiles without error or warning, even in Comeau.
Figure out what it prints, then verify by running it. If you were wrong, do you know
why?

#include <string>
#include <iostream>
#include <ostream>

using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Terje Slettebø
Guest





PostPosted: Sun Dec 05, 2004 7:50 pm    Post subject: Re: Fun with C++ Reply with quote



"Hyman Rosen" <hyrosen (AT) mail (DOT) com> wrote

Quote:
This represents an error I found in _C++ Coding Standards_ and makes a
nice puzzle.
The following program is legal and compiles without error or warning, even
in Comeau.
Figure out what it prints, then verify by running it. If you were wrong,
do you know
why?

#include #include #include
using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}

Intel C++ gave a hint when compiling at the highest warning level:

Test.cpp(12): remark #1419: external declaration in primary source file
string s = abc(), abc();
^
(The "^" sign is under the second "abc()")

From what I can tell, with "string s = abc(), abc();" you're declaring a
string variable "s" (and initialising it to the result of the "abc()"
function), as well as (re)declaring the function "string abc()" (the second
use of "abc()"). It's not using the comma operator; the defined comma
operator is just a red herring.

Nice puzzle, though. Smile (And a good entry in a C++ obfuscation contest.
Wink )

Regards,

Terje



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Sun Dec 05, 2004 7:56 pm    Post subject: Re: Fun with C++ Reply with quote



Hyman Rosen wrote:
Quote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.

Could you please reveal which section of this document is affected?

Quote:
Figure out what it prints, then verify by running it.

string s = abc(), abc();

It prints "abc", because the second abc() in the line above declares the
function abc instead of actually calling it and is equivalent to:

string s = abc();
string abc();

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Hairgrove
Guest





PostPosted: Sun Dec 05, 2004 7:57 pm    Post subject: Re: Fun with C++ Reply with quote

On 5 Dec 2004 06:16:22 -0500, Hyman Rosen <hyrosen (AT) mail (DOT) com> wrote:

Quote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.
The following program is legal and compiles without error or warning, even in Comeau.
Figure out what it prints, then verify by running it. If you were wrong, do you know
why?

#include #include #include
using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}

Interesting. If I run it exactly as above, the result is "abc". If I
change the first line inside of main() to this:

string s = (abc(), abc());

I get "abcabc", which is what I expected.

(compiler used for test: Borland C++ 5.5.1)

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Vinzenz Feenstra
Guest





PostPosted: Sun Dec 05, 2004 8:03 pm    Post subject: Re: Fun with C++ Reply with quote

Hyman Rosen wrote:
Quote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.
The following program is legal and compiles without error or warning, even in Comeau.
Figure out what it prints, then verify by running it. If you were wrong, do you know
why?

#include #include #include
using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}

Nice,

I didn't thought that it would be "abc" *g* and I had figure out why. I
don't know the standard en detail, so I asked some friends.

Now I know it is the operator= <-- ^^ it's binding got more strength
than operator,


BR Vinzenz

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Dec 06, 2004 9:33 am    Post subject: Fun with C++ Reply with quote

Vinzenz Feenstra <evilissimo (AT) web (DOT) de> wrote

Quote:
Hyman Rosen wrote:
This represents an error I found in _C++ Coding Standards_ and
makes a nice puzzle. The following program is legal and compiles
without error or warning, even in Comeau. Figure out what it
prints, then verify by running it. If you were wrong, do you know
why?

#include #include #include
using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}

Nice,

I didn't thought that it would be "abc" *g* and I had figure out why. I
don't know the standard en detail, so I asked some friends.

Now I know it is the operator= <-- ^^ it's binding got more strength
than operator,

You need to choose your friends more carefully when it comes to C++
advice. The problem has nothing to do with binding, and there's not a
single operator= (assignment) in the entire program. Nor, for that
matter, is there a use of the comma operator. Which is, of course, the
key to the problem.

In fact, the first line of main is the equivalent of:

string s( string( abc() ) ) ;
extern string abc() ;

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alex Vinokur
Guest





PostPosted: Mon Dec 06, 2004 9:41 am    Post subject: Re: Fun with C++ Reply with quote


"Maciej Sobczak" <no.spam (AT) no (DOT) spam.com> wrote

Quote:
Hyman Rosen wrote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.

Could you please reveal which section of this document is affected?

Figure out what it prints, then verify by running it.

-----------------------------------
string s = abc(), abc();

Yes, actually the line below is as follows:
string s = abc(), abc(void);

Unfortunaly, C++ doesn't require 'foo(void)' declaration for foo() that takes no arguments.
-----------------------------------
Quote:

It prints "abc", because the second abc() in the line above declares the
function abc instead of actually calling it and is equivalent to:

string s = abc();
string abc();

[snip]




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Dec 06, 2004 8:45 pm    Post subject: Re: Fun with C++ Reply with quote

In article <5MAsd.8884$ZK6.5090@trndny07>, Hyman Rosen
<hyrosen (AT) mail (DOT) com> writes
Quote:
This represents an error I found in _C++ Coding Standards_ and makes a
nice puzzle.
The following program is legal and compiles without error or warning,
even in Comeau.
Figure out what it prints, then verify by running it. If you were
wrong, do you know
why?

#include #include #include
using namespace std;

string abc() { return "abc"; }
string operator,(const string &a, const string &b) { return a + b; }

int main()
{
string s = abc(), abc();
cout << s << "n";
}


What do you think the defect is? In the context of a declarator a comma
is a punctuator (or I have always believed it is). Overloading operator,
has no more impact on declaration syntax than overloading operator= has
on the initialisation syntax.

string s = abc(), abc();

declares two things:

1) variable s which designates an object of type string initialised with
the return value of abc()

2) a function named abc that takes no arguments and returns a string by
value.

The latter is just a redeclaration (perfectly OK) of the function
already defined earlier in the same TU.



--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Mon Dec 06, 2004 8:46 pm    Post subject: Re: Fun with C++ Reply with quote

In article <covaei$84q$1 (AT) online (DOT) de>, Vinzenz Feenstra
<evilissimo (AT) web (DOT) de> writes
Quote:
I didn't thought that it would be "abc" *g* and I had figure out why. I
don't know the standard en detail, so I asked some friends.

Now I know it is the operator= <-- ^^ it's binding got more strength
than operator,

Then you knew more than your friends because you knew you were ignorant,
they apparently know something that is not true.

The declaration:

string s = abc(), abc();

does not contain either an operator= or an operator, despite appearances
to the contrary. The '=' is part of an initialisation syntax and is not
an assignment. The ',' is just punctuation and is not a sequence
operator.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Mon Dec 06, 2004 11:54 pm    Post subject: Re: Fun with C++ Reply with quote

Maciej Sobczak wrote:
Quote:
Hyman Rosen wrote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.
Could you please reveal which section of this document is affected?

Sorry, I think I meant _Guidelines_, not _Standards_.
The new Sutter/Alexandrescu book.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Mang
Guest





PostPosted: Mon Dec 06, 2004 11:57 pm    Post subject: Re: Fun with C++ Reply with quote


<kanze (AT) gabi-soft (DOT) fr> schrieb im Newsbeitrag
news:d6652001.0412060108.7d84a9cc (AT) posting (DOT) google.com...
Quote:
Vinzenz Feenstra <evilissimo (AT) web (DOT) de> wrote in message
news:<covaei$84q$1 (AT) online (DOT) de>...

Now I know it is the operator= <-- ^^ it's binding got more strength
than operator,

You need to choose your friends more carefully when it comes to C++
advice. The problem has nothing to do with binding, and there's not a
single operator= (assignment) in the entire program. Nor, for that
matter, is there a use of the comma operator. Which is, of course, the
key to the problem.

In fact, the first line of main is the equivalent of:

string s( string( abc() ) ) ;


Fun with C++, part 2 :-)


It's the equivalent of:

string s( (string( abc() ) ) ) ;



Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Tue Dec 07, 2004 12:38 am    Post subject: Re: Fun with C++ Reply with quote


"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message

Quote:
Then you knew more than your friends because you knew you were ignorant,
they apparently know something that is not true.

You can't know that I don't love you,
I said, you can't know that I don't love you,
Because to know that p, p must be true.

(From a blues I wrote while studying epistemology)

Jonathan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Colin Smith
Guest





PostPosted: Tue Dec 07, 2004 12:38 am    Post subject: Re: Fun with C++ Reply with quote

Hello,

The explanation is interesting, but I'm not sure that it explains why
the result is the same if you change the line to

string s;
s = abc(), abc();

At least on g++ (v. 3.3.2), the printed result is still 'abc'.

Maciej Sobczak wrote:
Quote:
Hyman Rosen wrote:
This represents an error I found in _C++ Coding Standards_ and makes a nice puzzle.

Could you please reveal which section of this document is affected?

Figure out what it prints, then verify by running it.

string s = abc(), abc();

It prints "abc", because the second abc() in the line above declares the
function abc instead of actually calling it and is equivalent to:

string s = abc();
string abc();


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Hyman Rosen
Guest





PostPosted: Tue Dec 07, 2004 12:41 am    Post subject: Re: Fun with C++ Reply with quote

Francis Glassborow wrote:
Quote:
What do you think the defect is?

The defect is in the S/A book, which contains similar code
and says that the comma operator is involved. It's talking
about evaluation order issues when the comma operator is
user-defined.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Terje Slettebø
Guest





PostPosted: Tue Dec 07, 2004 12:41 am    Post subject: Re: Fun with C++ Reply with quote

"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> wrote

Quote:

"Maciej Sobczak" <no.spam (AT) no (DOT) spam.com> wrote

Hyman Rosen wrote:
This represents an error I found in _C++ Coding Standards_ and makes
a nice puzzle.

Could you please reveal which section of this document is affected?

Figure out what it prints, then verify by running it.

-----------------------------------
string s = abc(), abc();

Yes, actually the line below is as follows:
string s = abc(), abc(void);

Unfortunaly, C++ doesn't require 'foo(void)' declaration for foo() that
takes no arguments.


Unfortunately? What's a more natural notation for indicating that a function
takes no argument, than... an empty parameter list?

The reason f(void) was even considered, early in the development of C++, is
that in C, _unfortunately_, f() meant that the function could take any
number of arguments... In an attempt to stay compatible with C, and yet
provide stronger type checking, f(void) was considered for a notation
indicating a function taking no arguments. However, it was, fortunately
(IMO), rejected, with characterisation of it as being an "abomination".
Therefore, a break with C was done at this point, to not create an ugly
workaround like f(void).

"void" is still needed for the return type, to denote no return value, and
in C, the implicit return type was int, and a break with C was not done on
this point. Besides, in templates (with the return type as a template
parameter), you need something to denote the return type, anyway, and using
"void" for no return value creates a quite regular system.

Regards,

Terje



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.