Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Test for learners

Andrey Tarasevich
Guest
 
Posts: n/a
#1: Sep 5 '08
Stefan Ram wrote:
Quote:
I have devised two tests for learners of C++:
>
1.) When you want to test whether something is an
expression within a correct program, put a pair of
parentheses around it. If it is an expression, the
program will still be correct and the behavior of the
program will not change.
Hm... I'm not sure about the intended scope of this rule. Say in this
declaration

int i;

I can put a pair of parentheses around 'i'

int (i);

and get an equivalent program. This still doesn't mean that this
specific 'i' in this specific context is an expression. It is not. Sure,
out of context 'i' is a valid expression, but in the above context it is
a declarator.

BTW, is your rule supposed to work both ways? I.e. if one can't put the
braces around it, does that mean it is not an expression? If so, then it
is not true as well. For example, in this context

struct A { int i; };

struct B : A {
void foo() {
int A::*p = &A::i; // 'A::i' is an expression in this context
}
};

'A::i' is an expression. However, if I put braces around it as in

struct B : A {
void foo() {
int A::*p = &(A::i); // ERROR
}
};

the program becomes ill-formed.
Quote:
2.) When you want to test whether something is a statement
within a correct program, put a pair of braces around it.
If it is a statement, the program will still be correct
and the behavior of the program will not change.
In C++ (as opposed to C, for example) declarations form declaration
_statements_. Juha already gave you an counterexample where extra braces
break the code.

Andrey Tarasevich
Guest
 
Posts: n/a
#2: Sep 5 '08

re: Re: Test for learners


Andrey Tarasevich wrote:
Quote:
...
BTW, is your rule supposed to work both ways? I.e. if one can't put the
braces around it, does that mean it is not an expression? If so, then it
is not true as well. For example, in this context
>
struct A { int i; };
>
struct B : A {
void foo() {
int A::*p = &A::i; // 'A::i' is an expression in this context
}
};
>
'A::i' is an expression. However, if I put braces around it as in
>
struct B : A {
void foo() {
int A::*p = &(A::i); // ERROR
}
};
>
the program becomes ill-formed.
...
.... although a pedantic person might argue that while 'A::i' is indeed a
valid expression in 'B::foo', nevertheless 'A::i' in '&A::i' is _not_ a
sub-expression, but rather a mere 'qualified-id'. There would probably
be no reason to even consider this pedantic issue, if not for your rule :)
Default User
Guest
 
Posts: n/a
#3: Sep 6 '08

re: Re: Test for learners


Stefan Ram wrote:
Quote:
Andrey Tarasevich <andreytarasevich@hotmail.comwrites:
Quote:
BTW, is your rule supposed to work both ways? I.e. if one can't
put the braces around it, does that mean it is not an expression?
>
I was not sure about this, too.
>
Now, I see, that these rules are not valid always, so I will
have to use wording like »often« for these rules.
Frankly, I'd drop them altogether. Even if they were 100% reliable, I
don't think they are useful to a student. As they aren't even
consistently true, their utility is further dimished.



Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Closed Thread