码迷,mamicode.com
首页 > 编程语言 > 详细

C++教程之auto关键字的使用

时间:2015-03-12 17:20:39      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:c++   lambda   auto   

一、auto关键字的前世

从C语言开始,auto关键字就被当作是一个变量的存储类型修饰符,表示自动变量(局部变量)。它不能被单独使用,否则编译器会给出警告。

#include <stdio.h>

int main()
{
        int a = 123;
        auto int b = 234;
        auto c = 345;

        printf("a = %d, b = %d, c = %d\n", a, b, c);
        return 0;
}

编译运行结果:

$ gcc main.c
main.c:7:7: warning: type specifier missing, defaults to ‘int‘ [-Wimplicit-int]
        auto c = 345;
        ~~~~ ^
1 warning generated.
$ ./a.out 
a = 123, b = 234, c = 345

二、auto关键字的今生

C++ 11标准中,添加了新的类型推导特性,考虑到auto关键字很少使用,就给它重新赋予了功能——申明类型由编译器推导的变量。在C++ 11中,使用auto定义的变量不能使用其它类型修饰符修饰,该变量的类型由编译器根据初始化数据自动确定。auto类型的变量必须进行初始化。

#include <iostream>

int main()
{
        int a = 21;
        auto b = a;

        //typeid可以获取变量或者数据类型的名字
        std::cout << typeid(b).name() << std::endl;

        return 0;
}

1 .使用C++ 98标准进行编译,会出现警告:

$ clang++ main.cpp -std=c++98
main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension
      [-Wc++11-extensions]
        auto b = a;
        ^
1 warning generated.
$ ./a.out 
i                   #输出结果为整数类型

改用C++ 11进行编译:

$ clang++ main.cpp -std=c++11
$ ./a.out 
i

2 .但是如果还是将auto作为存储类型修饰符使用,则在C++ 11标准下会出现警告:

#include <iostream>

int main()
{
        int a = 21;
        auto int b = a;

        //typeid可以获取变量或者数据类型的名字
        std::cout << typeid(b).name() << std::endl;

        return 0;
}

使用C++ 98编译:

$ clang++ main.cpp -std=c++98
$ ./a.out 
i

改用C++ 11编译,出现警告,不再允许作为存储类型修饰符使用:

$ clang++ main.cpp -std=c++11
main.cpp:6:2: warning: ‘auto‘ storage class specifier is not permitted in C++11,
      and will not be supported in future releases [-Wauto-storage-class]
        auto int b;
        ^~~~~
1 warning generated.

3 .必须要对auto类型的变量进行初始化,C++ 98中不能单独使用auto定义变量。

$ clang++ main.cpp -std=c++98
main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension
      [-Wc++11-extensions]
        auto b;
        ^
main.cpp:6:7: error: declaration of variable ‘b‘ with type ‘auto‘ requires an
      initializer
        auto b;
             ^
1 warning and 1 error generated.
$ clang++ main.cpp 
main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension
      [-Wc++11-extensions]
        auto b;
        ^
main.cpp:6:7: error: declaration of variable ‘b‘ with type ‘auto‘ requires an
      initializer
        auto b;
             ^
1 warning and 1 error generated.

三、扩展

在C++中还能使用decltype来获取变量或者表达式的类型并用来定义新的变量。

#include <iostream>

int main()
{
        int a = 21;
        decltype(a) b;

        std::cout << typeid(b).name() << std::endl;

        return 0;
}

编译运行结果:

$ clang++ main.cpp -std=c++98
$ ./a.out 
i
$ clang++ main.cpp -std=c++11
$ ./a.out 
i

需要注意的是该标准只是改变了C++中auto的用法,但是并没有影响auto在C语言中的使用!

本文档由长沙戴维营教育整理。

C++教程之auto关键字的使用

标签:c++   lambda   auto   

原文地址:http://blog.csdn.net/diveinedu/article/details/44222317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!