简体   繁体   中英

No error on missing return for inlined member (VS2013)

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++;
}

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.

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