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

前端 未结 2 1667

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() {}
         


        
2条回答
  •  暖寄归人
    2021-01-12 08:06

    As to why GCC allows it - sure because the Standard requires it to be valid. Building the transitive closure to the rationale of the rule in the Standard, I'm pretty sure that GCC allows this because it's useful in the event of templates

    template
    typename std::result_of::type call(F f) {
      return f();
    }
    
    int main() {
       std::cout << call([]{ return 42; }) << std::endl;
       call([]{ std::cout << "I'm just an outputtor!" << std::endl; });
    }
    

    As you see, call did not need to do a special case for void in the return statement. Sort of similar to how x.~T() is allowed even if T ends up as int.

提交回复
热议问题