auto *的类型推导规则是什么? [英] What are the type deduction rules for auto*?

查看:441
本文介绍了auto *的类型推导规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

auto*的类型推导规则是什么?

What are the type deduction rules for auto*?

请考虑以下内容:

int x = 64;
int* px = &x;

auto* v1 = &x;    // auto => ???    ok v1 is int* ...
auto* v2 = px;    // auto => ???    is v2 int*  ?
auto* v3 = &px;   // auto => ???    is v3 int** ?

只是为了澄清我的问题,如果我们将类型推演分为两个步骤:

Just to clarify my question if we split the type deduction into two steps:

  1. 在没有(*)的情况下推导"auto"本身的类型……然后
  2. 添加(*)后推导对象的类型(v1v2v3)
  1. Deducing the type of "auto" itself without (*) ... then
  2. Deducing the type of the object (v1, v2 and v3) after adding the (*)

所以我的两个问题是:

  1. 在没有(*)的情况下可以推断出auto是什么?
  2. v2是指向int(int*)的指针,还是v3指向指针(int**)的指针吗?
  1. What will auto be deduced to without the (*) ?
  2. Will v2 be pointer to int (int*) and v3 pointer to pointer (int**) ?

推荐答案

如果您知道模板类型推导,您几乎会知道auto 类型推导的全部内容. .因为自动类型推导的工作方式类似于模板类型推导.

If you know template type deduction you will know almost all there is to auto type deduction. Because auto type deduction works like template type deduction.

使用auto声明变量时,auto在模板中充当T,类型说明符充当参数类型:

When a variable is declared using auto, then auto acts as T in a template, and the type specifier acts as the parameter type:

const auto i = 20;

将翻译为:

template<typename T>
void func(const T param) { ... }
//        ^^^^^^^

参考:

const auto& j = i;

翻译为:

template<typename T>
void func(const T& param) 
//        ^^^^^^^^


与指针相同:


With pointers, it's the same:

auto* v1 = &x;

成为

template<typename T>
void func(T* param)

由于xint,所以auto* == int*.
而且auto* v2 = px;也是int*

Since x is an int, then auto* == int*.
And auto* v2 = px; is also int*

现在,您拥有的第三个:

Now, the third one you have:

auto* v3 = &px;

成为int**,因为您正在获取指针的地址.

Becomes int** since you're taking the address of the pointer.

template<typename T>
void func(T** param)
//        ^^^

查看自动类型的简便方法是使用其他人提到的typeid()函数.
但是我喜欢使用<boost/type_index.hpp>正确显示类型:

A handy way to see the type of auto is to use what others have mentioned, the typeid() function.
But I like to use <boost/type_index.hpp> to show the type correctly:

#include <iostream>
#include <boost/type_index.hpp>
using namespace std;
using namespace boost::typeindex;

int main()
{
    int x = 64;
    int* px = &x;

    auto* v1 = &x;
    auto* v2 = px;
    auto* v3 = &px;
    cout << type_id_with_cvr<decltype(v1)>().pretty_name() << '\n';
    cout << type_id_with_cvr<decltype(v2)>().pretty_name() << '\n';
    cout << type_id_with_cvr<decltype(v3)>().pretty_name() << '\n';
}

哪个输出:

int*
int*
int**


自动类型推导和模板类型推导之间有一个重要区别,即std::initializer_list<>


There is one important difference between auto type deduction and template type deduction, namely std::initializer_list<>

考虑以下示例:

auto i = 1;   // int
auto j(1);    // int
auto k = { 1 }// std::initializer_list<int> !
auto l { 1 }  // std::initializer_list<int> !

如您所见,将brace初始值设定项与auto一起使用可能会很麻烦.
但是,您可以在花括号前手动输入类型,以确保类型正确,但是我看不到重点:

As you see, using brace initializer with auto can be trouble.
You can however manually write the type before the braces to ensure that the type is correct but I don't see the point in that:

auto i = int{ 1 }; // type is int

新的自动规则已在Clang 3.8中实现,因此可以将auto-listing-initialization与auto(即将使用的标准)一起使用

There are new auto rules that have been implemented already in Clang 3.8 that makes it possible to use direct-list-initialization with auto (upcoming standard)

这篇关于auto *的类型推导规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆