简体   繁体   中英

Does C++ allow default return types for functions?

In C the following horror is valid:

myFunc()
{
  return 42;  // return type defaults to int.
}

But, what about in C++? I can't find a reference to it either way...

My compiler (Codegear C++Builder 2007) currently accepts it without warning, but I've had comments that this is an error in C++.

It's ill-formed in C++. Meaning that it doesn't compile with a standard conforming compiler. Paragraph 7.1.5/4 in Annex C of the Standard explains the change "Banning implicit int".

Implicit return types are valid in C89, but a lot of compilers warn about it.

They are not valid in C++, nor in C99.

So, it's definitely 'ill formed' C++, but it seems many compilers accept it with a warning at best.

  • Codegear C++Builder 2007: No error or warning at all
  • G++: Requires -W -Wall to generate warning , or -pedantic to generate error (Piotr)
  • MSVC 8: produces an error ( tfinniga )
  • others...?

Please add to/correct this list!

这不是合法的C ++,但是一些编译器会以静默方式或使用诊断方式接受它。

As posted, it is ill-formed. MSVC 8 gives the following error:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM