Wednesday, January 25, 2006

travel, meeting, snow, and broken code

Today I finally booked my travel to Brussels next month. In the afternoon I had a refreshing meeting of the faculty council. In the evening it started snowing a bit and thus I designed the following broken code:

namespace n {
    class c;
    void f(c&);
};

void f(n::c& v) {
    f(v);
}

Explain why this is broken code in C++.

2 comments:

Unknown said...

Can you elaborate? Am I to assume this is all the code there is, or are some symbols declared elsewhere?

I guess if the f function at the bottom was supposed to be the one at the top, it would be defined in the namespace as well, so I guess the original intention of the code was to call another function also named "f" that lives in a namespace, which will never happen because the current function's scope is matched before the namespace, and thus you have an endless recursion.

You'd have to say n::f(v); for it to call the other function.

Robert said...

This is all the code the compiler is given. It is possible that some declared symbols here are implemented in a different translation unit but this does not make any difference to this question.

It was not supposed that the two mentioned functions are the same. And the problem is not a endless recursion.

And after that your conclusions go more and more to the completely wrong direction.

So sorry, you have to return the cookie from the last question! ;-)