Reputation: 1834
The following code only throws a "missing return value" error for the outlined member, GetIN. The inlined one throws no error. If I use the method in the class I get an error for both. This seems completely wrong to me. Is there anything in the standard about the compiler ignoring things like return values for inlined methods? (this is compiled with Visual Studio 2013)
class MyClass
{
public :
MyClass()
: m_i(0)
{}
int GetI() { m_i++; }
int GetIN();
private:
int m_i;
};
int main()
{
return 0;
}
int MyClass::GetIN()
{
m_i++;
}
Upvotes: 2
Views: 238
Reputation: 355207
C++11 §6.6.3[stmt.return]/2 states:
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
The behavior is undefined, so the compiler is not required to diagnose the error.
Why does the compiler not diagnose the error? Because the function GetI
is defined inline in the class definition, it is implicitly declared inline. The compiler will only generate code for inline functions if they are actually used. Your program does not use the GetI
function, so the compiler generates no code for it, and thus the compiler does not detect the missing return statement.
If you modified your program to use the GetI
function, the compiler would have to generate code for the function and would then detect the missing return statement.
The function GetIN
is not inline, so the compiler must generate code for it because it is only defined in that one place.
Upvotes: 4