c++ - 如何将匿名 C++ union 与匿名结构一起使用

标签 c++ struct unions

我需要语法方面的帮助来访问结构中的 union ,如下所示。编译器提示说我需要在下面命名“innerStruct”定义,而不是使用匿名内部结构。有人可以解释规则以及我如何初始化构造函数字段并命名位字段元素。我有一个 live coliru demo 以显示代码。

不幸的是,代码无法编译,因为它指示以下错误:

g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In constructor 'Foo::Foo(uint8_t, uint8_t)':
main.cpp:38:11: error: class 'Foo' does not have any field named 'asUint8'
         , asUint8(aBitFields)
           ^
main.cpp: In function 'std::ostream& operator<<(std::ostream&, const Foo&)':
main.cpp:54:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
                                                                                   ^
main.cpp:55:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
                                                                                   ^
main.cpp:56:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
                                                                                   ^
main.cpp:57:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
                                                                                   ^

struct Foo {
    // fields
    uint8_t mType;
    union innerUnion_t {
        struct innerStruct_t {
            uint8_t mA : 2;
            uint8_t mB : 1;
            uint8_t mC : 2;
            uint8_t mD : 3;
        } innerStruct;
        uint8_t asUint8;
    } innerUnion;

    // constructor
    explicit Foo() = default;

    // constructor
    explicit Foo(
        const uint8_t aType,
        const uint8_t aBitFields)
        : mType(aType)
        , asUint8(aBitFields)
    {}

    /**
     * Stream insert operator<p>
     *
     * @param os     [in,out] output stream
     * @param rhs    [in] Foo to send to the output
     *               stream.
     *
     * @return a reference to the updated stream
     */
    friend std::ostream& operator<<(
        std::ostream& os, const Foo& rhs) {
        os  << "Foo"
            << ": type[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.mType
            << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
            << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
            << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
            << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
            << "]";
        return os;
    }
};

编辑

注意:按照建议的答案对结构定义稍作更改,我还有一个额外的问题,即初始化或引用位字段 mA -> mD 的语法应该是什么,我在构造函数中尝试使用以下语法,但它也不编译。我试图命名 innerUnion 并明确地将其命名的嵌套结构称为 innerUnion.innerStruct.mA(1) - 请参阅 second live demo

它给出了以下错误:

main.cpp: In constructor 'Foo::Foo(uint8_t, uint8_t)':
main.cpp:39:21: error: expected '(' before '.' token
         , innerUnion.innerStruct.mA(1)
                     ^
main.cpp:39:21: error: expected '{' before '.' token

最佳答案

你只需要删除代码中的一些词:

#include <memory>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

struct Foo {
    // fields
    uint8_t mType;
    union {
        struct {
            uint8_t mA : 2;
            uint8_t mB : 1;
            uint8_t mC : 2;
            uint8_t mD : 3;
        } innerStruct;
        uint8_t asUint8;
    };

    // constructor
    explicit Foo() = default;

    // constructor
    explicit Foo(
        const uint8_t aType,
        const uint8_t aBitFields)
        : mType(aType)
        , asUint8(aBitFields)
    {}

    /**
     * Stream insert operator<p>
     *
     * @param os     [in,out] output stream
     * @param rhs    [in] Foo to send to the output
     *               stream.
     *
     * @return a reference to the updated stream
     */
    friend std::ostream& operator<<(
        std::ostream& os, const Foo& rhs) {
        os  << "Foo"
            << ": type[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.mType
            << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
            << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
            << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
            << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
            << "]";
        return os;
    }
};

int main()
{
    std::vector<std::string> words = {
        "Hello", "from", "GCC", __VERSION__, "!"
    };
    std::cout << words << std::endl;

    auto pFoo = std::make_unique<Foo>();
    std::cout << *pFoo << std::endl;
}

关于c++ - 如何将匿名 C++ union 与匿名结构一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984321/

相关文章:

swift - 如何快速复制一个只有一个不同字段的结构

c - 通过指针访问 C union 成员

c++ - 为什么预构建的 Protocol Buffer 不包含任何头文件

c++ - gcc和c++ 17的重载解析失败

c++ - 哪个链接更快?许多小的 .so 文件还是几个大的 .so 文件?

c++ - Qt5如何将DATA_BLOB转换为QByteArray

c - 结构接口(interface)

c++ - 在 struct C++ 中使用 char* 或 char []

c - 如何使用包含结构的 union ?

c - C 中的递归 union 定义