[英]Difference between Auto and Void?
I am reading the C++ Primer Book (fifth edition) and I have a question upon the book i am reading. 我正在阅读C ++ Primer Book(第五版),我对正在阅读的书有疑问。 It says:
它说:
The type
void*
is a special pointer type that can hold the address of any object.void*
类型是一种特殊的指针类型,可以保存任何对象的地址。 Like any other pointer, avoid*
pointer holds an address, but the type of the object at that address is unknown.与任何其他指针一样,
void*
指针保存一个地址,但该地址处对象的类型未知。
Ok, so I understand that but… I have many contradictions to the statement. 好的,所以我理解但是...我对这个陈述有很多矛盾。 First of all, can't you use
auto
? 首先,你能用
auto
吗? Doesn't it do the same thing as void
? 它不是和
void
吗? Meaning aren't 意思不是
void *somePtr;
and 和
auto *somePtr;
the same? 相同?
Second of all it says the type of the attached address is unknown. 其次,它说附加地址的类型是未知的。 Can't you use
typeid
to get the type? 你不能使用
typeid
获取类型吗? Like this: 像这样:
int a = 5;
auto *somePtr = 5;
std::cout << typeid(*somePtr).name() << std::endl;
Did you try running your examples through a compiler? 您是否尝试通过编译器运行示例? As it happens, neither
碰巧,两者都没有
auto * p; // error
nor 也不
int a = 0;
void * p = &a;
std::cout << typeid(*p) << '\n'; // error
will compile while both 将两者都编译
void * p;
and 和
int a = 0;
auto * p = &a;
std::cout << typeid(*p) << '\n';
are fine. 没事。
To give an informal and easy to remember analogy for void *
and auto *
, think of void * p
as telling the compiler 为了给
void *
和auto *
提供一个非正式且易于记忆的类比,请将void * p
视为告诉编译器
Don't care about what
p
pointer points to, I'll handle it myself (at run-time).不关心
p
指针指向的是什么,我将自己处理它(在运行时)。
whereas auto * p = (expression)
tells the compiler 而
auto * p = (expression)
告诉编译器
Please figure out for me (at compile-time) what (pointer) type
(expression)
evaluates to and make the type ofp
be accordingly.请为我(在编译时)计算出什么(指针)类型
(expression)
计算结果并使p
的类型相应。
As a matter of fact, auto *
is rarely useful. 事实上,
auto *
很少有用。 You can simply write auto
instead and if the initializing expression is a pointer type, the auto
will be deduced to be a pointer, too. 您可以简单地编写
auto
,如果初始化表达式是指针类型,则auto
也将被推断为指针。
auto
uses compile-time type inference : the variable has a definite type which is its correct type, however the type is inferred from its value. auto
使用编译时类型推断 :变量具有明确的类型,它是正确的类型,但是类型是从其值推断出来的。 Throughout the lifetime of the auto
variable, the compiler will ensure that it is being used in accordance with its actual inferred type 在
auto
变量的整个生命周期中,编译器将确保根据其实际推断类型使用它
void*
can indeed hold the address of any object, but as opposed to auto
, you must explicitly convert the void*
pointer to a different type before using it. void*
确实可以保存任何对象的地址,但与auto
相反,您必须在使用之前将void*
指针显式转换为其他类型。 Unlike auto
that conversion is explicit, and unlike auto
the compiler cannot warn about improper usage. 与
auto
不同,转换是显式的,与auto
不同,编译器无法警告不正确的使用。
From the compiler's viewpoint : 从编译器的角度来看 :
auto
uses the same rules of inference as template argument deduction . auto
使用与模板参数推导相同的推理规则。 It basically tells the compiler: "Deduce the type from the initializer." void *
tells the compiler: "Here is a pointer pointing to an object whose type cannot possibly be known at compile time." void *
告诉编译器: “这是一个指向一个对象的指针,该对象的类型在编译时是不可能知道的。” For auto
the actual type is deduced at compile time , so the compiler can warn about possible misuse. 对于
auto
,实际类型是在编译时推断出来的 ,因此编译器可以警告可能的误用。
For void *
the actual type is deduced in code by the programmer using a reinterpret_cast
to cast to a known pointer type, typically at the point of execution of that segment of code. 对于
void *
, 程序员使用reinterpret_cast
在代码中推导出实际类型,以强制转换为已知的指针类型,通常是在执行该段代码时。
From the programmer's viewpoint : 从程序员的角度来看 :
auto
it and let you figure out the rest. auto
它,让你弄明白其余的。 (And it looks smaller than the type I was about to write. Except for int
.) int
。) void *
. void *
。 I'll figure out the type at runtime myself (in code...) when I need it. So NO auto
is NOT equivalent to void
. 所以NO
auto
不等于void
。 void
means nothing . void
意义 。 In fact you can't even declare a variable of type void
. 实际上你甚至不能声明一个
void
类型的变量。 void *
however is a special type of pointer that can point to anything! void *
然而,它是一种特殊类型的指针 ,可以指向任何东西! And auto *
is redundant, just use plain old auto
instead. 而
auto *
是多余的,只需使用普通的旧auto
。
In your example, 在你的例子中,
int a = 5; auto *somePtr = 5; std::cout << typeid(*somePtr).name(); << std::endl;
You assigned an address 5
to somePtr
which is illegal. 您为
somePtr
分配了一个非法的地址5
。 Needlessly stating that *somePtr
is also illegal in that context. 毫无疑问地说,
*somePtr
在这种情况下也是非法的。 Although auto
is probably deduced to an int
, which is what you wanted to print. 虽然
auto
可能推断为int
,这是你想要打印的。
Well with modern C++ techniques, you rarely see void *
s and reinterpret_cast
s or any raw pointers for instance except deep within any library implementation internals. 使用现代C ++技术,除了深入任何库实现内部之外,您很少看到
void *
s和reinterpret_cast
或任何原始指针。
To answer your first question, 要回答你的第一个问题,
void and auto are not same. void和auto不一样。
When you declare a variable of auto, it means it takes the data types of the value assigned
. When you declare a variable of auto, it means it takes the data types of the value assigned
。
for ex: 对于前:
auto a = 5 // 'a' is of type integer auto a = 5 //'a'的类型为整数
auto a = 5.0 // 'a' is of type float etc., auto a = 5.0 //'a'的类型为float等,
The actual difference can be observed while dereferencing. 解除引用时可以观察到实际差异。
The above statement "the actual difference can be observed while dereferencing" answers your second question. 上面的陈述“解除引用时可以观察到实际差异”回答了你的第二个问题。
void* cannot be deferenced, whereas auto* can be deferenced void *不能被引用,而auto *可以被引用
In case auto variable it will take data type of variable from whom it is getting assgnied. 如果是自动变量,它将采用变量的数据类型。 For example Auto obj = class_obj.
例如,Auto obj = class_obj。 Here data type of class_obj became data type of obj.
这里class_obj的数据类型变为obj的数据类型。 You no need explicetly remember and type cast.
你不需要明确记住并输入强制转换。 In case void * you need to know data type and do type cast explicetly.
如果是void *,您需要知道数据类型并进行explicetly类型转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.