使用与unique_ptr的联合

jac*_*ild 15 c++ struct unique-ptr unions c++11

当我尝试std :: move或std :: make_unique时,尝试在union中使用unique_ptr会给我一个段错误.

#include <iostream>
#include <memory>

union myUnion{
    struct{std::unique_ptr<float> upFloat;}structUpFloat;
    struct{std::unique_ptr<int> upInt;}structUpInt;
    myUnion(){}
    ~myUnion(){}
};
struct myStruct{
    int x;
    myUnion num;

};
int main()
{
    myStruct aStruct, bStruct;
    aStruct.x = 1;
    bStruct.x = 2;

    auto upF = std::make_unique<float>(3.14);
    auto upI = std::make_unique<int>(3);

    aStruct.num.structUpFloat.upFloat = std::move(upF);
    bStruct.num.structUpInt.upInt = std::move(upI);

    std::cout << "aStruct float = " << *aStruct.num.structUpFloat.upFloat << std::endl;
    std::cout << "bStruct int = " << *bStruct.num.structUpInt.upInt << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,使用普通指针按预期工作:

#include <iostream>
#include <memory>

union myUnion{
    struct{float *pFloat;}structPFloat;
    struct{int *pInt;}structPInt;
    myUnion(){}
    ~myUnion(){}
};
struct myStruct{
    int x;
    myUnion num;

};
int main()
{
    myStruct aStruct, bStruct;
    aStruct.x = 1;
    bStruct.x = 2;

    auto upF = std::make_unique<float>(3.14);
    auto upI = std::make_unique<int>(3);

    aStruct.num.structPFloat.pFloat = upF.get();
    bStruct.num.structPInt.pInt = upI.get();

    std::cout << "aStruct float = " << *aStruct.num.structPFloat.pFloat << std::endl;
    std::cout << "bStruct int = " << *bStruct.num.structPInt.pInt << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是使用clang.3.4.2或gcc.4.9.0.所以我假设我在这里做错了.任何帮助,将不胜感激.

编辑:

好的,所以分享我确定的代码可能是件好事.非常感谢所有指导我使用placement new管理变体成员指针生命周期的人.

#include <memory>
#include <iostream>
#include <vector>
struct myStruct
{
public:
    union
    {
        std::unique_ptr<float> upFloat;
        std::unique_ptr<int> upInt;
    };
    enum class unionType {f, i,none} type = unionType::none; // Keep it sane
    myStruct(){}
    myStruct(std::unique_ptr<float> p)
    {
        new (&upFloat) std::unique_ptr<float>{std::move(p)};
        type = unionType::f;
    }
    myStruct(std::unique_ptr<int> p)
    {
        new (&upInt) std::unique_ptr<int>{std::move(p)};
        type = unionType::i;
    }
    ~myStruct()
    {
        switch (type)
        {
            case unionType::f: upFloat.~unique_ptr<float>(); break;
            case unionType::i: upInt.~unique_ptr<int>(); break;
        }
    }
};

int main()
{
    std::vector<std::unique_ptr<myStruct>> structVec;
    structVec.push_back(std::make_unique<myStruct>(std::make_unique<float>(3.14f)));
    structVec.push_back(std::make_unique<myStruct>(std::make_unique<int>(739)));
    structVec.push_back(std::make_unique<myStruct>());
    structVec.push_back(std::make_unique<myStruct>(std::make_unique<float>(8.95f)));
    structVec.push_back(std::make_unique<myStruct>(std::make_unique<int>(3)));
    structVec.push_back(std::make_unique<myStruct>());

    for(auto &a: structVec)
    {
        if(a->type == myStruct::unionType::none)
        {
            std::cout << "Struct Has Unallocated Union" << std::endl;
        }
        else if(a->type == myStruct::unionType::f)
        {
            std::cout << "Struct float = " << *a->upFloat << std::endl;
        }
        else
        {
            std::cout << "Struct int = " << *a->upInt << std::endl;
        }
        std::cout << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

结构浮点数= 3.14

Struct int = 739

Struct具有未分配的联盟

结构浮点数= 8.95

Struct int = 3

Struct具有未分配的联盟

Ben*_*igt 13

更改联合的活动成员需要特别注意对象的生命周期.C++标准说(9.5p4):

注意:通常,必须使用显式析构函数调用和放置新运算符来更改联合的活动成员.

当成员是普通的旧数据时,它通常"正常工作",即使您没有调用构造函数(使用放置new)和析构函数.这是因为具有简单初始化的对象的生命周期开始于"获得存储时"足够大并且正确对齐,并且联合提供了这一点.

现在你已经有了非平凡的构造函数和析构函数的成员.获得存储时,它们的生命周期不会开始,您必须完成初始化.这意味着安置新的.跳过析构函数调用也不安全,如果这些析构函数会有程序依赖的副作用(并且unique_ptr析构函数具有解除分配目标的副作用),则会得到未定义的行为.

因此,您在生命周期尚未开始的成员上调用移动赋值运算符.这是未定义的行为.


Jar*_*d42 10

对于不受限制的联合,你必须自己管理一些构造/破坏.

以下可能有所帮助

union myUnion{
    std::unique_ptr<float> upFloat;
    std::unique_ptr<int> upInt;

    myUnion(){ new (&upFloat) std::unique_ptr<float>{};}
    ~myUnion() {}
};

class myStruct
{
public:
    ~myStruct()
    {
        destroy();
    }

    void destroy()
    {
        switch (type)
        {
            case unionType::f: num.upFloat.~unique_ptr<float>(); break;
            case unionType::i: num.upInt.~unique_ptr<int>(); break;
        }
    }

    void set(std::unique_ptr<int> p)
    {
        destroy();
        new (&num.upInt) std::unique_ptr<int>{std::move(p)};
        type = unionType::i;
    }
    void set(std::unique_ptr<float> p)
    {
        destroy();
        new (&num.upFloat) std::unique_ptr<float>{std::move(p)};
        type = unionType::f;
    }

public:
    enum class unionType {f, i} type = unionType::f; // match the default constructor of enum
    myUnion num;
};

int main()
{
    myStruct aStruct, bStruct;

    aStruct.set(std::make_unique<float>(3.14f));
    bStruct.set(std::make_unique<int>(3));

    std::cout << "aStruct float = " << *aStruct.num.upFloat << std::endl;
    std::cout << "bStruct int = " << *bStruct.num.upInt << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在C++ 17中,您可以使用std::variant而不是自己的结构

  • 如果您使用匿名联合,这将不那么脆弱,使整个类成为变体(类似联合)的类。现在可以在没有提供所有所需逻辑的包装器的情况下使用联合。 (2认同)