Why is this not an annoying analysis?

This is basically a continuation of this question about the most unpleasant analysis. I can understand that this is due to the ambiguity between the declaration of the function and the definition of the variable.

But in Como online, I'm just tired of the following.

class T{

public:

    T(int i){
    }

    int fun1(){
        return 1;
    }

};

int main()
{
    T myT(10); // I thought it'd be a function declaration that takes an int and returns a type T
    myT.fun1(); // and a compiler error out here.
} 

But it compiles fine, and there were no errors. I looked at the standard documents, but could not come to reasoning.

So what am I missing here?

+3
source share
3 answers

10 cannot be a parameter type name, so it must be a variable declaration.

The compiler must choose to declare a function when it can do it, but in many cases it cannot and does not have ambiguity.

+6
source

10 .:)

Vexing Parse:

T myT(T());
// T() gets interpreted as function pointer argument to a function returning T();
// This is equivalent to:
T myT(T (*fn)());

Vexing Parse :

unsigned char c = 42;
T myT(int(c));
// int(c) gets interpreted as an int argument called c.
// This is equivalent to:
T myT(int c);
+7

, , , :

T myT(T());

:

#include <iostream>

struct T { int f() { return 1; } };

int main(int argc, char** argv) {
    T t(T());
    std::cout << t.f() << '\n';
    return 0;
}

, :

  • myT - T, T;
  • myT - , T T(), , T.

The latter interpretation is standard by default, so a compiler error results from trying to use the newly declared function, as if it were the object you were expecting.

See the Wikipedia article about this.

+3
source

All Articles