On 25.02.2017 2:10, Steve Keller wrote:
> Paavo Helde <
myfir...@osa.pri.ee> writes:
>
>> It's a general rule in C++ to use the matching identifier from the
>> closest scope (class scope in this case). Otherwise you could not
>> write things like
>>
>> #include <complex.h>
>>
>> int foo(int arg) {
>> return arg + 1; // ambiguity! is this the local parameter
>> // or the arg() function from <complex.h>?
>> }
>
> Yes, of course. But since in my code the call to exec(s) with Stmt *s
> doesn't match the definition of FooStmt::exec() I wouldn't count this
> as a match and continue matching in a wider scope and would find the
> actually matching global exec(const Stmt *).
Yes, this might seem like a good idea, but over the years I have learned
the hard way that trying to be too clever is about the worst thing a
language can do. The reason is that if the language is too clever then
the programmer is not able any more to figure out how it works and thus
it loses control over how to achieve that it wants.
C++ already has function overloading, Koenig lookup and other things
where it has arguably gone too clever, there is no need to make it even
more "cleverer".
>> Basically you want to have a special kind of pointer where
>> dereferencing null is ignored. In C++ one writes a new class for such
>> things:
>>
>> class ExecPtr {
>> const Stmt* s;
>> // ...
>> public:
>> void exec() const {
>> if (s) {
>> s->exec();
>> }
>> }
>> };
>>
>> class FooStmt : public Stmt {
>> ExecPtr s;
>> public:
>> virtual void exec() const {
>> s.exec();
>> }
>> };
>
> Hm, this looks like an interesting solution. I'll consider this. But
> for consistency I'd have to do it for class Expr {} with Expr::eval()
> also, although I don't have the same problem there. What I dislike a
> little is that in s.exec() the member s doesn't really look like a
> pointer anymore.
You can make it look like s->exec() with some more effort, though by
some reason I suspect you are wasting your pedantry in wrong areas.
There are no abstract ideals to which your code should conform.
Instead, programming is an engineering discipline, meaning everything
serves a purpose. Consistency of the code and markup is needed only for
the maintainer programmer (including yourself 6 months ahead) to
understand the program so he can easily maintain or refactor it. In this
sense, writing some decent unit tests is probably much more helpful than
a minor stylistic consistency.