Wednesday, August 15, 2007

From the CCC Camp


Two issues I encountered on the Chaos Communication Camp:

operator&&

It is not possible to have a self-defined operator&& with the same semantics as the built-in operator&&. In the statement foo && bar bar is evaluated if and only if foo is true. But if foo is of a self-defined type with an overloaded operator&& bar gets evaluated no matter what foo evaluates to. This is because bar must be passed as an argument to the operator&& function and late evaluation is not supported in C++. So much for operator overloading is for having drop-in replacements of built-in types.

compiler DoS

A standard complient way to launch a DoS on your C++ compiler is the following code:
template <class T>
struct Loop {
    Loop<T*> operator->();
};

int main()
{
    Loop<int> i;
    i->foo();
}
This is because of the chaining property of operator->. The ISO standard says in 13.3.1.2.8:
[...] When operator-> returns, the operator-> is applied to the value returned, with the original second operand [footnote] If the value returned by the operator-> function has class type, this may result in selecting and calling another operator->.
In the above example this chain of operator-> functions never ends and the compiler stays busy for ever to figure out which foo is to be called, if existent at all.

No comments:

Post a Comment