为什么这不是一个令人烦恼的解析?

lia*_*iaK 3 c++ most-vexing-parse

基本上这是关于最烦恼的解析的这个问题的后续.我可以理解这是由于函数声明和变量定义之间的模糊性.

但在Comeau在线,我只是厌倦了以下.

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.
} 
Run Code Online (Sandbox Code Playgroud)

但它编译得很好而且没有错误.我查看了标准文档但无法进行推理.

那么,我在这里错过了什么?

Boa*_*niv 7

因为10不是一种类型.:)

这将是一个最令人烦恼的解析:

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

另一种最令人烦恼的解析是这一个:

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);
Run Code Online (Sandbox Code Playgroud)


Bo *_*son 6

10不能是参数类型名称,因此必须是变量声明.

编译器必须在可以做到的时候选择一个函数声明,但在很多情况下它不能这样做,并且没有歧义.