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

标签 c++ zos xlc

在 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/

相关文章:

c++ - 类破坏段错误

c++ - 从 int 到 char* 没有字符串,和 fstream 不方便

c - AIX 6.0 环境下的 xlC 编译问题

makefile - 使用 xlc 编译器的 GNU make

c - IBM XL C 编译器 - 如何扩展用户包含而不是系统包含

c++ - 为什么我的异常在某些配置上被捕获而在其他配置上却没有?

c++ - 无法理解 int 和用户定义类型之间的名称查找差异 - 可能与 ADL 相关

windows - Rexx - 方括号用法

mainframe - 如何在 JCL 中引用最新的物理顺序 (PS) 文件

assembly - 如何在 IBM 中使用显式寻址 (HLASM)