Visual Studio C++ compiler weird behaviour

后端 未结 2 490
我在风中等你
我在风中等你 2020-12-14 15:07

I\'m just curious to know why this small piece of code compiles correctly (and without warnings) in Visual Studio. Maybe the result is the same with GCC and Clang,

2条回答
  •  -上瘾入骨i
    2020-12-14 15:35

    Because it defines a variable of type T:

    http://coliru.stacked-crooked.com/a/d420870b1a6490d7

    #include 
    
    struct T {
        int t;
        T() : t(0) {}
    };
    
    int main() {
        T(i_do_not_exist);
        i_do_not_exist.t = 120;
        std::cout << i_do_not_exist.t;
        return 0;
    }
    

    The above example looks silly, but this syntax is allowed for a reason.

    A better example is:

    int func1();
    namespace A
    {
       void func1(int);
       struct X {
           friend int (::func1)();
       };
    }
    

    Probably other examples could be found.

提交回复
热议问题