- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 reading about 之后Martin Uecker 的 ICE_P
谓词的标准 C11 版本,我尝试用纯 C++ 实现它。 C11版本,使用_Generic
选择如下:
#define ICE_P(x) _Generic((1? (void *) ((x)*0) : (int *) 0), int*: 1, void*: 0)
C++ 的明显做法是用模板和decltype
替换_Generic
,例如:
template<typename T> struct is_ice_helper;
template<> struct is_ice_helper<void*> { enum { value = false }; };
template<> struct is_ice_helper<int*> { enum { value = true }; };
#define ICE_P(x) (is_ice_helper<decltype(1? (void *) ((x)*0) : (int *) 0)>::value)
但是,它失败了 simplest test .为什么它不能检测整型常量表达式?
最佳答案
这个问题很微妙。确定条件表达式的指针操作数的复合类型的规范在 C++ 中与 C 中的类似,所以它开始看起来很有希望:
(N4659) [expr.cond]
7 Lvalue-to-rvalue, array-to-pointer, and function-to-pointerstandard conversions are performed on the second and third operands.After those conversions, one of the following shall hold:
[...]
One or both of the second and third operands have pointer type; pointer conversions, function pointer conversions, and qualificationconversions are performed to bring them to their composite pointertype (Clause [expr]). The result is of the composite pointer type.
[...]
复合指针类型的缩减指定如下:
(N4659) [expr]
5 The composite pointer type of two operands p1 and p2 havingtypes T1 and T2, respectively, where at least one is a pointer orpointer to member type or
std::nullptr_t
, is:
- if both p1 and p2 are null pointer constants,
std::nullptr_t
;- if either p1 or p2 is a null pointer constant, T2 or T1, respectively;
- if T1 or T2 is “pointer to cv1 void” and the other type is “pointer to cv2 T”, where T is an object type or void, “pointer to cv12 void”,where cv12 is the union of cv1 and cv2;
- [...]
因此,我们的 ICE_P
宏的结果取决于我们按顺序检查上面的哪一颗子弹。鉴于我们如何定义 is_ice_helper
,我们知道复合类型不是 nullptr_t
,否则我们会命中第一颗子弹,并且会由于缺少模板特化而出错.所以我们必须击中第 3 个项目符号,使谓词报告为假。这一切似乎都取决于空指针常量的定义。
(N4659) [conv.ptr] (emphasis mine)
1 A null pointer constant is an integer literal with valuezero or a prvalue of type
std::nullptr_t
. A null pointerconstant can be converted to a pointer type; the result is the nullpointer value of that type and is distinguishable from every othervalue of object pointer or function pointer type. Such a conversion iscalled a null pointer conversion. Two null pointer values of the sametype shall compare equal. The conversion of a null pointer constant toa pointer to cv-qualified type is a single conversion, and not thesequence of a pointer conversion followed by a qualificationconversion. A null pointer constant of integral type can be convertedto a prvalue of typestd::nullptr_t
.
由于 (int*)0
不是上面定义的空指针常量,我们不符合 [expr]/5 的第一个项目符号。复合类型不是std::nullptr_t
。 (void *) ((x)*0)
既不是空指针常量,也不能变成空指针常量。删除转换(定义不允许的东西)给我们留下 (x)*0
。这是一个值为零的整数常量表达式。但它不是一个值为零的整数文字! C++中空指针常量的定义与C中的有所不同!
(N1570) 6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such anexpression cast to type
void *
, is called a null pointer constant. Ifa null pointer constant is converted to a pointer type, the resultingpointer, called a null pointer, is guaranteed to compare unequal to apointer to any object or function.
C 允许任意 值为零的常量表达式形成空指针常量,而 C++ 需要整数文字。鉴于 C++ 对计算各种文字类型的常量表达式的丰富支持,这似乎是一个不必要的限制。并且使上述 ICE_P
方法成为 C++ 中的非入门者。
关于c++ - 在纯 C++ 中实现 Linux 内核的 __is_constexpr (ICE_P) 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55780426/
总的来说,我对 Linux 内核和操作系统非常感兴趣。我想知道的是,内核的文件类型或扩展名是什么?它显然没有 .exe 或 .out 扩展名,因为它们用于安装在操作系统上的应用程序。 内核只是一个二进
我需要为 Raspbian Linux 内核添加一个自己的系统调用。现在我在搜索了大约 2 天以找到解决方案后陷入困境。 要加一个系统调用,我基本上是按照大纲来的( http://elinux.org
对于一个学术项目,我希望将源文件 (myfile.c) 添加到 kernel/目录,与exit.c相同的目录和 fork.c .构建系统似乎不会自动获取新文件,因为我在 myfile.c 中定义的函数
浏览器排行榜 浏览器市占率排行榜全球榜 。 浏览器市占率排行榜中国榜 -快科技 。 如果按照浏览器内核来看, Chromium 内核的市场占有率无疑是最大的,一家独大
给定一个进程或线程的任务结构,迭代属于同一进程的所有其他线程的习惯用法是什么? 最佳答案 Linux 不区分进程(任务)和线程。库调用 fork() 和 pthread_create() 使用相同的系
我正在用c(不是linux。完全从头开始)从头开始制作一个内核,但我遇到了一些问题。我有这个代码: #include "timer.h" int ms = 0; void timer_handler(
我正在从头开始制作一个 C 内核,我实际上只是从网站上复制了这段代码,因为我的代码无法工作,所以我很困惑。 void kmain(void) { const char *str = "my f
我不确定,如果我完全理解上述差异,所以我想自己解释一下,你可以打断我,只要我有错:“内核是创建内核线程的初始代码段。内核线程是由内核管理的进程。用户线程是进程的一部分。如果你有一个单线程进程,那么整个
看一下struct file 定义from this code Linux 内核版本 2.6.18。 我正在尝试比较代码中的两个 struct file 变量,并确定它们是否指的是同一个文件。该结构中
我试图在 Linux 启动时使嵌入式设备中的 LED 闪烁。基本上,LED 闪烁表明 Linux 正在启动。为了使 LED 闪烁,我正在做以下事情 在 init/main.c 中创建了一个全局定时器(
我有一些在 FreeBSD 和 Linux 上运行的特定硬件。 我必须做一个用户空间应用程序,它将使用内核/用户空间应用程序之间的共享内存与驱动程序一起工作。我的应用程序对来自用户空间的共享内存进行忙
我在哪里可以找到 linux 内核中相应函数的解释,特别是对于 ICMPv4? 例如:icmp_reply、icmp_send等 感谢您的帮助。 最好的,阿里木 最佳答案 探索 Linux 内核中的
我在 Linux Kernel 3.4 上工作,我有以下代码: /* Proximity sensor calibration values */ unsigned int als_kadc;
我正在阅读“罗伯特·洛夫 (Robert Love) 撰写的 Linux 内核开发第 3 版”,以大致了解 Linux 内核的工作原理..(2.6.2.3) 我对等待队列的工作方式感到困惑,例如这段代
我之前也问过同样的问题,但是我的帖子不知为何被删除了。 无论如何,我正在尝试使用 C++ 并编写一个允许我直接访问内存并向其中写入内容的程序。我听说我需要对内核做一些事情,因为它是连接操作系统和应用程
在尝试了解 Ruby 执行方法时,我找到了这篇关于在 Ruby 中运行命令的五种方法的博文 http://mentalized.net/journal/2010/03/08/5_ways_to_run
是否有 Linux 发行版(Minix 除外)包含良好的源代码文档?或者,是否有一些好的文档来描述一般的 Linux 源代码? 我已经下载了内核源代码,但是(不出所料)我有点不知所措,我想知道是否有一
有谁知道 linux 中的哪个函数或文件包含查找用于 bind() 系统调用的随机端口的算法?我到处寻找,在 Linux 源代码中找不到包含此算法的方法。 谢谢! 最佳答案 这是一段又长又复杂的代码,
前言 首先,对于有科班背景的读者,可以跳过本系列文章。这些文章的主要目的是通过简单易懂的汇总,帮助非科班出身的读者理解底层知识,进一步了解为什么在面试中会涉及这些底层问题。否则,某些概念将始终
CentOS7.2与CentOS6区别及特点 Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化。内核初始化的
我是一名优秀的程序员,十分优秀!