【问题标题】:Can the 'auto' keyword be used as a storage class specifier in C++11?'auto' 关键字可以用作 C++11 中的存储类说明符吗?
【发布时间】:2011-05-22 11:22:46
【问题描述】:

auto 关键字可以用作 C++11 中的存储类说明符吗?

以下代码在 C++11 中合法吗?

int main() {
   auto int x;
}

【问题讨论】:

    标签: c++ c++11 variable-declaration storage-class-specifier


    【解决方案1】:

    不,C++11 中的代码格式不正确。 C++11 中的auto 将用于从其初始化程序中推断变量的类型,并且不能用作存储类说明符。

    正确使用

    int main()
    {
       auto x = 12; // x is an int
       auto y = 12.3; // y is a double
    }
    

    【讨论】:

    • 所以现在是 C++ 11,是吗?我越来越糊涂了。我希望这能很快通过一些官方声明得到解决:)
    • @Space_C0wb0y :是的,官方名称很可能是 C++11 :)
    • @Space_C0wb0y:还没有,当 Sutter 被问到这个问题时,他说他宁愿等待(现在不必急于求成)该标准真正达成一致,然后再用它的 C++0x 配音确定的千禧年。
    • @Björn Pollex:只有 ISO 在 1 月之前完成。如果不是,那就不是。因此,C++0x 仍然是更明确的术语(恕我直言)。
    【解决方案2】:
    auto int x;
    

    是循环的——您实际上是在将类型声明为int。 鉴于您有这些信息 - 没有理由不简单地使用:

    int x;
    

    如果你想在范围内声明 x 另一个变量的类型,你可以使用decltype

    using sometype = float;
    sometype y;
    decltype(y) x;
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2021-07-11
      • 2016-08-26
      • 2012-01-30
      • 2012-08-24
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多