gpt4 book ai didi

c++ - 为什么 xlc++ 编译器会提示强制转换右值?

转载 作者:行者123 更新时间:2023-11-30 05:18:07 24 4
gpt4 key购买 nike

在 z/OS 上,pthread_t 类型是一个包含成员 char __[8]; 的结构。我试图将其从返回值转换为 int64_t。我收到以下错误:

CCN5216 (S) An expression of type "char [8]" cannot be converted to type "int64_t"

但是如果我使用临时变量 tmp 它会起作用。我可以使用 Visual Studio 2015 编译此代码(定义类似于 zos pthread_t 的自定义 mypthread 结构)而不会出现错误。你知道为什么 xlc++ 这个转换有问题吗? Actor 标准是否符合?

#define _OPEN_THREADS
#include <iostream>
#include <stdint.h>
#include <pthread.h>

pthread_t apr_os_thread_current() {
return pthread_t();
}

int64_t getId() {
pthread_t tmp = apr_os_thread_current();
return (int64_t) tmp.__; // ok
//return (int64_t) apr_os_thread_current().__; // CCN5216
//return reinterpret_cast<int64_t>(apr_os_thread_current().__); // CCN5216
}

int main() {
std::cout << getId() << std::endl;
return 0;
}

最佳答案

没有临时变量,数组是右值的一部分,这可能会抑制隐式数组到指针的转换,需要将 char[8] 重新解释为 int64_t直接。

引入临时变量(左值)可以在 tmp.__ 上实现数组到指针的转换,然后将指针强制转换为 int64_t,没有任何“问题” ,实际上返回了一个虚假值。换句话说,您的工作/编译代码等同于以下内容:

return (int64_t) &tmp.__[0]; // ok ???

这只是一个假设,您可以按如下方式检查:

int64_t getId() {
pthread_t tmp = apr_os_thread_current();
int64_t result = (int64_t) tmp.__;
if ( result == (int64_t) &tmp.__[0] )
std::cerr << "oops!" << std::endl;
return result;
}

关于c++ - 为什么 xlc++ 编译器会提示强制转换右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com