C++11 自动推导auto

落爺英雄遲暮 提交于 2020-03-18 23:11:58

C++11 自动推导auto

 

C++11中引入的auto主要有两种用途:自动类型推导和返回值占位。

auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。 

 

自动类型推导  

auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推导,可以大大简化我们的编程工作。

auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响。另外,auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <map>


// 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,不使用auto将很难确定变量的类型
template <typename T, typename U>
void Multiply(T t, U u)
{
    auto v = t * u; // 使用auto后,将由编译器自动进行确定
}

class student
{
public:
    static int var1;
    //auto var2; 错误,非静态成员变量
    //static auto var3; 错误,需要初始值
};

int student::var1 = 10;

//void fun(auto x = 1) {}  错误,auto函数参数,有些编译器无法通过编译


void mytest()
{
    //auto a; 错误,没有初始化表达式,无法推断出a的类型
    //auto int a1 = 0; 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。

    // 1. 自动帮助推导类型
    auto a = 10; // a ---> int
    auto c = 'A'; // c ---> char
    auto s("hello"); // s --> const char *
    // 2. 类型冗长
    std::map<int, std::map<int, int> > map_;
    std::map<int, std::map<int, int> >::const_iterator itr1 = map_.begin();
    const auto itr2 = map_.begin();
    auto ptr = []() // ptr ---> void ptr()
    {
        std::cout << "mytest ..." << std::endl;
    }; // lambda 表达式
    
    char x[3];
    auto y = x; // y ---> char * // auto会退化成指向数组的指针,除非被声明为引用 
    // auto z[3] = x; 错误,auto数组,无法通过编译

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

 

2.使用注意事项

1、我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
auto k = 5;
auto* pK = new auto(k);
auto** ppK = new auto(&k);
const auto n = 6;

2、用auto声明的变量必须初始化
auto m; // m should be intialized

3、auto不能与其他类型组合连用
auto int p; // 这是旧auto的做法。

4、函数和模板参数不能被声明为auto
void MyFunction(auto parameter){} // no auto as method argument
template<auto T> // utter nonsense - not allowed
void Fun(T t){}

5、定义在堆上的变量,使用了auto的表达式必须被初始化
int* p = new auto(0); //fine
int* pp = new auto(); // should be initialized
auto x = new auto(); // no intializer
auto* y = new auto(9); // Fine. Here y is a int*
auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)

6、以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
int value = 123;
auto x2 = (auto)value; // no casting using auto
auto x3 = static_cast<auto>(value); // same as above

7、定义在一个auto序列的变量必须始终推导成同一类型
auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this

8、auto不能自动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
const int i = 99;
auto j = i; // j is int, rather than const int
j = 100 // Fine. As j is not constant
// Now let us try to have reference
auto& k = i; // Now k is const int&
k = 100; // Error. k is constant
// Similarly with volatile qualifer

9、auto会退化成指向数组的指针,除非被声明为引用
int a[9];
auto j = a;
cout<<typeid(j).name()<<endl; // This will print int*
auto& k = a;
cout<<typeid(k).name()<<endl; // This will print int [9]

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!