C++11 | 自动类型推断——auto

来源:互联网 发布:南京金鹰有mac专柜吗 编辑:程序博客网 时间:2024/04/28 04:13

C++11里引入了类型推断功能,auto和decltype,写代码更方便了。有人说,仅仅为了使用auto,就应该切换到C++11上来。

所以,这次来简单说说auto的用法。

auto的简单用法

最简单的用法如下:

    auto num = 5;

此时, 变量num的类型是int。编译器会自动根据初始化赋给num的值来推断num的类型。如果你写成下面这样,num的类型就变了:

    auto num = 5ll;

这个num的类型可能是__int64(我在VS2013下测试的)。

像上面的用法,体现不出来auto的优势。下面的才可以:

    std::map<std::string, std::list<int>> records = {        { "ZhangSan", {88, 60, 98} },         { "LiSi", { 36, 21, 63 } }    };    auto iter = records.find("Lisi");

如果不用auto的话,代码就是这样的:

    std::map<std::string, std::list<int>>::iterator iter = records.find("Lisi");

多敲多少字啊!

为了检测用auto声明的变量的类型,可以参考“C++11 | 运行时类型识别(RTTI)”里介绍过的type_info类。代码类似下面:

    auto num = 5;    const std::type_info &ti = typeid(num);    std::cout << "num\'s type: " << ti.name() << std::endl;

auto容易混淆的地方

用auto声明的变量,其类型是:赋值给那个变量的值的类型。这话说起来有点绕,结合代码说一下。

    auto num = 5;

像这个num,类型就是int。

    auto iter = records.find("Lisi");

而这个iter,其类型就是map的find()方法的返回值的类型,也就是一个迭代器。可以使用type_info来验证一下。

现在应该说明白了。但是,如果把auto和range-based for loop结合起来,就容易发生混淆了。

举个简单的例子来对比一下。代码如下:

int _tmain(int argc, _TCHAR* argv[]){    auto num = 5;    const std::type_info &ti = typeid(num);    std::cout << "num\'s type: " << ti.name() << std::endl;    auto & numRef = num;    const std::type_info &tiNumRef = typeid(numRef);    std::cout << "numRef\'s type: " << tiNumRef.name() << std::endl;    std::list<int> numbers = { 1, 3};    auto it = numbers.begin();    const std::type_info &tiIter = typeid(it);    std::cout << "it\'s type: " << tiIter.name() << std::endl;    for (auto n : numbers)    {        const std::type_info &tiN = typeid(n);        std::cout << "n = " << n << " n\'s type: " << tiN.name() << std::endl;    }    std::map<std::string, int> name2age = { { "Mary", 28 }, { "John", 32 } };    for (auto person : name2age)    {        const std::type_info &tiPerson = typeid(person);        std::cout << "age = " << person.second << " person\'s type: " << tiPerson.name() << std::endl;    }}

上面代码运行的结果如下图所示:

我用蓝笔标注了type_info提供的类型名,好对比。

如你所见,it的类型是class std::_List_iterator<...>,这是在range-based for loop之外,符合我们预期。

而在for之内呢,则是int。这实际上是由range-based for loop决定的。在range-based for loop这种for循环里,你看到的变量,实际上是iterator解引用后的类型,所以对于list<int>这个容器,for(auto n: numbers)这个语句里的n,类型就是int。

你再注意我红笔标注的部分,那是使用range-based for loop遍历map时打印出来的,那里的person变量,类型是struct std::pair<...>,贼长贼长。

理解了这一点,auto的用法就基本上无碍了。


这篇提到了C++11的range-based for loop特性,还有示例代码中有统一初始化语法的应用,回头有时间也聊聊。

参考阅读:

  • C++11 | 运行时类型识别(RTTI)
2 0
原创粉丝点击