lundi 28 décembre 2015

Function call missing argument list warning

I'm reading over "The C++ Programming Language - Fourth Edition" and I was typing up a simple exercise just to get a hang of C++ syntax and I accidentally stumbled across something that made me raise an eyebrow. In short, I forgot to add () on accept in main:

bool accept()
{
    cout << "Do you want to proceed (y or n)?\n";

    char answer = 0;
    cin >> answer;

    if (answer == 'y')
    {
        return true;
    }
    return false;

}

int main()
{
    accept;
}

This runs and compiles and produces (in VS2015) a

C4551 - function call missing argument list

I've found myself reading lambdas and a bunch of questions on SO that should be be closed because they are mostly asking to "debug my code."

I figured that if the code compiles and runs, and the function contains a blocking statement (waiting for user input) and a return type, that all the code would get executed as expected regardless of missing the parentheses; that is not the case though.

Additionally, I figured I would change the call to accept in main, to bool a = accept; cout << a; to try and prevent any optimization (if that was what was actually happening), and that did not call the accept() code either.

What I'm curious to know is:

  1. What is the call to accept getting compiled into?
  2. Why isn't the code in accept getting called
  3. Why is this only a warning, and not an error (I know I can change configuration to display it as error, I'm more so questioning how this is accepted syntax by default when actual result differs from expected result so "dramatically?" This question might be opinion-based, omit it if you agree it is.)
  4. Running the bool a = accept; cout << a; code in main produces 1 as the output. How can this be when false is the default bool value (in C# at least) and there is nothing to return a true value since the accept code does not get executed?

Aucun commentaire:

Enregistrer un commentaire