简体   繁体   English

活跃成员(已标记)的工会

[英]Active member in (tagged) union

I've got a union I'm running a program verification software over, and would like to say something about which member is active. 我有一个联合会,我正在运行一个程序验证软件,并想说一下哪个成员处于活动状态。 So I've tried writing it as a tagged union: 因此,我尝试将其编写为带标签的联合:

struct my_struct {
    enum my_tag {v1, v2};
    struct{
        enum my_tag tag;
        union {
            type1 v1;
            type2 v2;
        }value;
    }my_taggedunion; 
};

and am running a program verification software on it. 并在其上运行程序验证软件。 I would like to add the requirement that, if my_taggedunion.tag == v1 then the active member in the union should be v1 . 我想添加一个要求,如果my_taggedunion.tag == v1则联合中的活动成员应为v1

I'm trying to write something like "if my_taggedunion.tag == v1 then my_taggedunion.active_member == v1 . What is the correct syntax for the conclusion? 我正在尝试编写类似“ if my_taggedunion.tag == v1然后my_taggedunion.active_member == v1 。结论的正确语法是什么?

There is no notion of an "active member" as far as C is concerned. 就C而言,没有“活跃成员”的概念。 Note that according to the ANSI C standard: 请注意,根据ANSI C标准:

If a pointer to a union is cast to the type of a pointer to a member, the result refers to that member. 如果将指向联合的指针转换为指向成员的指针的类型,则结果将引用该成员。

(see 3.5.2.1 of X3.159-1989 and A8.3 of Kernighan & Ritchie) (请参阅X3.159-1989的3.5.2.1和Kernighan&Ritchie的A8.3)

You have to keep track of the relevant part of the union yourself, which in your case you are doing through my_tag . 您必须自己跟踪联合的相关部分,在您的情况下,这是通过my_tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM