const union成员的用法是什么?他们不是没有意义吗? [英] What's the use of const union members? Aren't they quite pointless?

查看:190
本文介绍了const union成员的用法是什么?他们不是没有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对此答案的评论中,Koushik提出了非常有效的点

In the comments to this answer, Koushik raised a very valid point.

采取以下措施:

union U
{
    int x;
    const T y;
};

(我选择 T 在这里没有共同的初始布局兼容性序列,意味着每个 [C ++ 11:9.5 / 1] 在任何给定时间只有一个成员是活动的。 / sup>

(I choose T such that there is no common initial sequence of layout compatibility here, meaning only one member may be active at any given time per [C++11: 9.5/1].)

由于在同一时间只有一个成员处于活动状态(通过写入活动), code>不能在初始化后写入,这不是没有意义吗?我的意思是, y 只能从第一次写入 x 时读取, code> y 是初始化的成员。

Since only one member may be "active" at any one time (made active by writing to it), and y cannot be written to after initialisation, isn't this rather pointless? I mean, y can only be read from until the first time x is written to, and at that only if y was the initialised member.

有没有一些用例我缺少?

Is there some use case I'm missing? Or is this indeed a pretty pointless confluence of language features?

(This has been mentioned before)

推荐答案

下面是一个引用语义类型的例子其中您只想授予 const 权限。 union 用于从类型擦除函数返回的类变量数据类型。

Here's a contrived example of a reference-semantics type where you'd only want to grant const access to. The union is used in a variant-like data type returned from a "type-erasing" function.

#include <memory>

template<class T>
struct reference_semantics
{
public:
    reference_semantics(T* p ) : m(p) {}

    int observe() const { return *m; }
    void change(T p) { *m = p; }

private:
    T* m;
};

struct variant
{
    enum T { INT, DOUBLE } type;

    union U
    {
        reference_semantics<int> const i;
        reference_semantics<double> const d;

        U(int* p) : i(p) {}
        U(double* p) : d(p) {}
    } u;
};

#include <iostream>
std::ostream& operator<<(std::ostream& o, variant const& v)
{
    switch(v.type)
    {
        case variant::INT:
            return o << "INT: "<<v.u.i.observe();
        case variant::DOUBLE:
            return o << "DOUBLE: "<<v.u.d.observe();
    }
}

#include <string>

variant type_erased_access(std::string name)
{
    // imagine accesses to a map or so

    static double dval = 42.21;
    static int ival = 1729;

    if(name == "Lightness") return { variant::DOUBLE, &dval };
    else return { variant::INT, &ival };
}

int main()
{
    variant v0( type_erased_access("Lightness") );
    std::cout << v0 << "\n";
    variant v1( type_erased_access("Darkness") );
    std::cout << v1 << "\n";
}

想象现在代替 int double ,使用更大的数据类型,并且 reference_semantics 数据类型实际上提供了比只是返回值。

Imagine now that instead of int and double, much larger data types are used, and that the reference_semantics data type actually provides more functionality than just returning the value.

这可能是你想返回一个 reference_semantics< some_type> const 用于某些参数,但是对于其他参数是一个简单的 int 。在这种情况下,你的 union 甚至可能有const和非const成员。

It might even be possible that you want to return a reference_semantics<some_type> const for some arguments, but a plain int for others. In that case, your union might even have const and non-const members.

这篇关于const union成员的用法是什么?他们不是没有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆