简体   繁体   English

从std :: size_t *到long unsigned int的转换无效*

[英]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 charunsigned charchar 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.

相关问题 如何 std::variant<unsigned long, size_t, unsigned int></unsigned> - how to std::variant<unsigned long, size_t, unsigned int> “错误:在转换的常量表达式中不允许从'double'转换为'std::size_t'(又名'unsigned long')”是什么意思? - what does “ error: conversion from 'double' to 'std::size_t' (aka 'unsigned long') is not allowed in a converted constant expression” mean? 如何解决隐式转换丢失整数精度:&#39;size_t&#39;(又名&#39;unsigned long&#39;)到&#39;int&#39;警告? - How to solve the Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' warning? iOS-隐式转换将整数精度&#39;size_t&#39;(aka&#39;unsigned long&#39;)转换为&#39;int&#39; - iOS - implicit conversion loses integer precision 'size_t' (aka 'unsigned long') to 'int' 从size_t转换为unsigned int - converting from size_t to unsigned int 错误:从&#39;void *&#39;到&#39;test :: apr_size_t * {aka long unsigned int *}&#39;的无效转换&#39;[-fpermissive] - error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive] size_t的大小与unsigned int的比较 - size of size_t compared to unsigned int size_t到unsigned int(来自API函数) - size_t to unsigned int (from API function) 从&#39;:: size_t&#39;到&#39;int&#39;的转换需要缩小的转换 - conversion from '::size_t' to 'int' requires a narrowing conversion xcode 构建失败 隐式转换丢失 integer 精度:“size_t”(又名“unsigned long”)到“socklen_t”(又名“unsigned int”) - xcode build failing over Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'socklen_t' (aka 'unsigned int')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM