代码之家  ›  专栏  ›  技术社区  ›  geza

如果我将一个变量重新定义为auto,并且导出的类型相同,那么它的格式是否正确?[副本]

  •  11
  • geza  · 技术社区  · 6 年前

    int a;
    extern int b;
    auto b = a;
    

    格式是否正确?Clang成功地编译了它,但是GCC和MSVC没有。

    How to declare and define a static member with deduced type? )

    2 回复  |  直到 6 年前
        1
  •  3
  •   Community CDub    4 年前

    Tl;DR;

    [dcl.spec.auto] 并对推导的返回类型进行限制 [dcl.spec.auto]p11 添加了,否则没有限制,因此变量情况下不受限制。

    my more complete answer in the duplicate

        2
  •  5
  •   zneak    6 年前

    Clang , GCC , MSVC . (之前的回答是所有3个编译器都会拒绝构建它,但这是不正确的。)

    dcl.spec.auto 在混合 auto 类型说明符与其他类型说明符。然而,它 addresses it for function return types :

    auto f();
    auto f() { return 42; } // return type is int
    auto f();               // OK
    int f();                // error, cannot be overloaded with auto f()
    decltype(auto) f();     // error, auto and decltype(auto) don't match
    

    所以我的直觉是,这是标准中的一个疏忽,行为目前还没有明确规定,但如果/当它被明确规定时,就会有先例将其定为非法。(另一方面,变量不能重载,所以谁知道呢。)