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 

true exception-by-value demo =)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alexander Kamotsky
Guest





PostPosted: Fri Nov 12, 2004 12:03 pm    Post subject: true exception-by-value demo =) Reply with quote



Hi, all!

Recently I found a silly bug in my program. It was in a test module
which was written a long time ago and always passed ok (as every bug
should be =))

After figuring out how it could ever be compiled (and working!),
I've decided to decorate it in a joke demo.

It _is_ compilable (I wonder if you'll get a single warning):

#include <iostream>
#include <sstream>
#include <string>

typedef std::string FileName;

namespace Exception
{
struct CantOpen
{
CantOpen (const FileName& aFileName):fileName (aFileName) {}
FileName fileName;
};
std::ostream& operator<< (std::ostream& s, const CantOpen& co)
{
return s << "can't open: " << co.fileName << std::endl;
}
}

void demo_CantOpen (int whichToThrow)
{
FileName fn1("1.txt"),
fn2("2.txt");
try
{
if (whichToThrow == 1)
{
throw Exception::CantOpen(fn1);
}
else
{
throw Exception::CantOpen(fn2);
}
}

// here is magic:

catch ( Exception::CantOpen(fn1) )
{
std::cout << Exception::CantOpen(fn1);
}
catch (...)
{
std::cout << Exception::CantOpen(fn2);
}
}

int main (void)
{
demo_CantOpen (1);
demo_CantOpen (2);
}

// --- Program output :
// can't open: 1.txt
// can't open: 2.txt

=)

--
Best regards,
Alexander Kamotsky mailto:alexandroid (AT) p5com (DOT) com


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

Back to top
Michael Karcher
Guest





PostPosted: Fri Nov 12, 2004 3:39 pm    Post subject: Re: true exception-by-value demo =) Reply with quote



Alexander Kamotsky <alexandroid (AT) p5com (DOT) com> wrote:
Quote:
It _is_ compilable (I wonder if you'll get a single warning):
Only using -Wshadow (g++ 3.3). Nice example!


Quote:
// here is magic:
catch ( Exception::CantOpen(fn1) )
Of couse, it's the old declaration vs. initialization vs. function call

parsing problem. The parentheses are just to distract the reader from the
real meaning of this code. Compiler vendors could warn about type(name)
declarations IMHO.

Michael Karcher

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

Back to top
Bronek Kozicki
Guest





PostPosted: Fri Nov 12, 2004 3:43 pm    Post subject: Re: true exception-by-value demo =) Reply with quote



Alexander Kamotsky wrote:
Quote:
catch ( Exception::CantOpen(fn1) )

this looks like invalid exception declaration. Exception declaration
should be ... or type specifier and optionally declarator, while above
is definition of temporary object. It compiles, but I'd expect it to be
bug in (the few I tested) compilers.


B.

[ 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 Nov 15, 2004 4:32 pm    Post subject: Re: true exception-by-value demo =3D) Reply with quote

[email]Michael.Karcher (AT) writeme (DOT) com[/email] (Michael Karcher) wrote in message
news:<2vjqi0F2l4jf7U1 (AT) uni-berlin (DOT) de>...
Quote:
Alexander Kamotsky <alexandroid (AT) p5com (DOT) com> wrote:
It _is_ compilable (I wonder if you'll get a single warning):
Only using -Wshadow (g++ 3.3). Nice example!

// here is magic:
catch ( Exception::CantOpen(fn1) )

Of couse, it's the old declaration vs. initialization vs. function
call parsing problem.

Except that it isn't. The code is in a catch statement. It must be a
declaration, because nothing else is legal here.

Quote:
The parentheses are just to distract the reader from the real meaning
of this code. Compiler vendors could warn about type(name)
declarations IMHO.

The parentheses are obfuscation in this case, but I don't see anything
that the compiler should warn about. The context requires a definition;
an expression wouldn't be legal. The user has provided an expression.
Unless the compiler is warning about violations of coding guidelines,
what's to warn about?

Note that this is somewhat different from the usual case, where we have
a local function definition which could *also* be a definition of a
variable with initialization.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, 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
kanze@gabi-soft.fr
Guest





PostPosted: Mon Nov 15, 2004 4:33 pm    Post subject: Re: true exception-by-value demo =3D) Reply with quote

Bronek Kozicki <brok (AT) rubikon (DOT) pl> wrote

Quote:
Alexander Kamotsky wrote:
catch ( Exception::CantOpen(fn1) )

this looks like invalid exception declaration.

Looks perfectly valid to me. What's invalid about it?

Quote:
Exception declaration should be ... or type specifier and optionally
declarator, while above is definition of temporary object.

Where do you see a temporary object? There's a declaration of a
variable, which is exactly what is wanted in a catch clause.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, 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
Michael Karcher
Guest





PostPosted: Wed Nov 17, 2004 11:20 am    Post subject: Re: true exception-by-value demo =3D) Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
// here is magic:
catch ( Exception::CantOpen(fn1) )
Of couse, it's the old declaration vs. initialization vs. function
call parsing problem.
Except that it isn't. The code is in a catch statement. It must be a
declaration, because nothing else is legal here.

OK, the compiler's parser has no problem here, it expects a declaration and
fits the tokens into it. You are right. But the human parser reading the
code reads this as expression, because without context it *would* be an
expression. I don't like things to change it's meanings this way, just
because their original meaning is illegal at this point.

Quote:
The parentheses are just to distract the reader from the real meaning
of this code. Compiler vendors could warn about type(name)
declarations IMHO.
The parentheses are obfuscation in this case, but I don't see anything
that the compiler should warn about.

type(already_declared_name) is a legal expression everywhere except where
expressions are illegal. I would like a language where one thing can only
mean one thing, but that is impossible in C++, for historical reasons. So I
want at least a warning that tells me, that the code has been interpreted in
some probably unexpected way.

Quote:
Unless the compiler is warning about violations of coding guidelines,
what's to warn about?

Yes, you could call it a violation of a coding guideline. As well as
"if(i=1) { cout << "i is 1" << endl; }". There is nothing wrong with
assignments within if clauses. Nevertheless I get a warning on most
compilers for this example, because the writer probably intended to say
"i==1".

Michael Karcher

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

Back to top
Bronek Kozicki
Guest





PostPosted: Wed Nov 17, 2004 11:25 am    Post subject: Re: true exception-by-value demo =3D) Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
catch ( Exception::CantOpen(fn1) )
this looks like invalid exception declaration.
Looks perfectly valid to me. What's invalid about it?

and "fn1" is declarator here, as required by clause 15 (syntax of catch
statement)? Hm... I never used that form... but actually you might be
right.


B.

[ 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: Fri Nov 19, 2004 12:44 pm    Post subject: Re: true exception-by-value demo =3D) Reply with quote

Bronek Kozicki <brok (AT) rubikon (DOT) pl> wrote

Quote:
kanze (AT) gabi-soft (DOT) fr wrote:
catch ( Exception::CantOpen(fn1) )
this looks like invalid exception declaration.
Looks perfectly valid to me. What's invalid about it?

and "fn1" is declarator here, as required by clause 15 (syntax of catch
statement)?

"fn1" (or rather "(fn1)" is most certainly a declarator. And it has
nothing to do with this being a catch statement --
"Exception::CantOpen(fn1)" is a declaration any place it could be a
declaration. There are cases where this is confusing, because it could
also be an expression, and we typically expect an expression, but this
isn't the case in a catch statement.

--
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
kanze@gabi-soft.fr
Guest





PostPosted: Fri Nov 19, 2004 12:45 pm    Post subject: Re: true exception-by-value demo =3D) Reply with quote

[email]Michael.Karcher (AT) writeme (DOT) com[/email] (Michael Karcher) wrote in message
news:<2vutr4F2pns40U1 (AT) uni-berlin (DOT) de>...
Quote:
kanze (AT) gabi-soft (DOT) fr wrote:
// here is magic:
catch ( Exception::CantOpen(fn1) )
Of couse, it's the old declaration vs. initialization vs. function
call parsing problem.
Except that it isn't. The code is in a catch statement. It must
be a declaration, because nothing else is legal here.

OK, the compiler's parser has no problem here, it expects a
declaration and fits the tokens into it.

No. If it is an LR-parser, it analyses the tokens, finds a declaration,
and then finds that it is in a context where a declaration is legal.

Quote:
You are right. But the human parser reading the code reads this as
expression, because without context it *would* be an expression.

Actually, I think it is just the reverse. The human reader recognizes
immediately that it must be a declaration, because the human reader sees
the context, i.e. a catch block. An LR-parser will analyse first, see
something that could be either a declaration or an expression, and then
apply disambiguating rules to determine which.

Quote:
I don't like things to change it's meanings this way, just because
their original meaning is illegal at this point.

Nothing changes meaning. The token sequence "Exception::CantOpen(fn1)"
is a declaration, except in contexts where a declaration is illegal.
Try it:

Exception::CantOpen(fn1) ; // Declares a variable fn1
int f( Exception::CantOpen(fn1) ) ; // Declares a function parameter.

Only when a declaration is syntactically illegal is it an expression.

Quote:
The parentheses are just to distract the reader from the real
meaning of this code. Compiler vendors could warn about type(name)
declarations IMHO.

The parentheses are obfuscation in this case, but I don't see
anything that the compiler should warn about.

type(already_declared_name) is a legal expression everywhere except
where expressions are illegal.

No. You've got it backwards. It is a legal expression ONLY where a
declaration would be illegal.

Quote:
I would like a language where one thing can only mean one thing, but
that is impossible in C++, for historical reasons. So I want at least
a warning that tells me, that the code has been interpreted in some
probably unexpected way.

The question is: what is expected. In a catch clause, I expect a
declaration, so treating it as a declaration is not unexpected.

About the only case I see a warning as justified is when it occurs as a
function parameter in a local function declaration. Local function
declarations are rare enough to be unexpected, and there is no key word
(like catch) to signal them; local variables which are intialized by an
expression are IMHO far mor common and expected.

While not necessarilly justified, I wouldn't complain about a warning
anytime there would be an ambiguity, e.g. if the function declaration
wasn't local, but could also be interpreted as a variable definition.

I can't see a warning, however, when absolutly no ambiguity is possible.

--
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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
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.