[英]Invalid conversion from std::size_t* to long unsigned int*
On a raspberry pi - arv7l I'm compiling the following C++ program 在树莓派-arv7l上我正在编译以下C ++程序
#include <iostream>
void fun(unsigned long int* i)
{
std::cout << *i << std::endl;
}
int main()
{
std::size_t i = 1;
fun(&i);
return 0;
}
For the code above I'm getting the following error: 对于上面的代码,我收到以下错误:
a.cpp: In function 'int main()':
a.cpp:12:9: error: invalid conversion from 'std::size_t* {aka unsigned int*}' to 'long unsigned int*' [-fpermissive]
fun(&i);
^~
a.cpp:3:6: note: initializing argument 1 of 'void fun(long unsigned int*)'
void fun(unsigned long int* i)
^~~
I understand that on 32-bit systems size_t is 32 bit and may be different than unsigned long
but still this should compile? 据我所知,在32位系统上,size_t是32位,可能与
unsigned long
不同,但是这应该编译吗?
With regards to the sizes of the various types: 关于各种类型的尺寸:
sizeof(size_t): 4
sizeof(unsigned int): 4
sizeof(unsigned long int): 4
sizeof(size_t*): 4
sizeof(unsigned int*): 4
sizeof(unsigned long int*): 4
System: Linux turtlebot 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux 系统:Linux turtlebot 4.14.79-v7 +#1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU / Linux
Compiler: gcc-6.3 编译器:gcc-6.3
I understand that on 32-bit systems size_t is 32 bit and may be different than unsigned long but still this should compile?
据我所知,在32位系统上,size_t是32位,可能与unsigned long不同,但是这应该编译吗?
This is not correct. 这是不正确的。 You are correct that the types have the same size, but size is not what makes a type a type.
您是正确的,类型具有相同的大小,但大小不是使类型成为类型的原因。 For instance we have
signed char
, unsigned char
, and char
. 例如,我们
signed char
, unsigned char
和char
。 All three have a size of 1
, but all three are distinct types named by the standard. 所有这三个有一个规模
1
,但所有这三个是不同的类型由标准命名。
In your case size_t*
is a unsigned int*
and since that is a different type than a long unsigned int*
, you cannot implicitly cast it to one. 在你的情况下,
size_t*
是一个unsigned int*
,因为它是一个与long unsigned int*
不同的类型,你不能隐式地将它强制转换为一个。
Why should it compile? 为什么要编译? You have an
unsigned int
(on your system a std::size_t
), and you are pretending that it is a long unsigned int
. 你有一个
unsigned int
(在你的系统上是一个std::size_t
),你假装它是一个long unsigned int
。
But it isn't, regardless of size, so it doesn't work. 但它不是,无论大小,所以它不起作用。
There is more to types than their width. 类型多于宽度。
Use consistent types for portable code. 对可移植代码使用一致类型。
A unsigned long int*
and a unsigned int*
are not the same type, even though the sizes are the same. unsigned long int*
和unsigned int*
的类型不同,即使大小相同。
There are implicit conversions between integer types, however this does not apply to pointers to integer types which is why you get the error. 还有整数类型之间的隐式转换,但是这并不适用于指针为整数类型这就是为什么你的错误。
You'll need to change either the type of the variable or the type of the function parameter for them to match. 您需要更改变量的类型或函数参数的类型以使它们匹配。
In C++ types with different size modifiers are different even if same size. 在C ++类型中,具有不同大小的修饰符即使相同大小也是不同的。 There are systems with sizeof(char) == sizeof(short int) == sizeof (int) == sizeof(long int) ...
有些系统的sizeof(char)== sizeof(short int)== sizeof(int)== sizeof(long int)...
Add a std::static_assert to your code to ensure that both types are equal size and than you may call the function like this: 在代码中添加一个std :: static_assert,以确保两种类型的大小相同,并且可以像这样调用函数:
fun(&reinterpret_cast<unsigned long&>(i));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.