2

Possible Duplicate:
Why can you return from a non-void function without returning a value without producing a compiler error?

Consider the real code:

std::ostream &operator << (std::ostream &stream, const sqc_var &var)
{
    switch (var.type()) 
    {
        case sqc_var::type_number:
            return stream << var.as_number();
        case sqc_var::type_string:
            return stream << var.as_string();
        case sqc_var::type_object:
            stream << "{";
            for (auto &child : var.children())
            {
                stream << child.name() << ": " << child.value() << " "; // <-- crash there
            }
            stream <<  " }";
    }

    //// return stream; <-- originaly no return was there.
}

This code will crash program if var have an type_object type after first recursive call of operator << because it returns nothing when the reference to std::ostream expected.

Clang 3.1 even does not print warning.

Modern GCC and MSVC print warning but compile it.

Why C++ compiler allows such erroneous code?

Also I'll be glad to see any references to C++ standard which allows such code or explanation when it can be usefull.

4

0 回答 0