Why is g++ allowing me to treat this void-function as anything but?

c++, c++11, gcc

Solution

That's allowed by the standard (§6.6.3/3)

A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.

Problem

Why does the following compile in GCC 4.8 (`g++`)? Isn't it completely ill-formed? ``` void test(int x) { return test(3); } int main() {} ``` - I'm trying to use the result of calling `test`, which does not exist - I'm trying to return a value from `test` Both should be fundamentally impossible — not just UB, as far as I can recall — with a `void` return type. The only warning I get is about `x` being unused, not even anything about a non-standard implicit return type being added. Live demo

Original source

Related problems