简体   繁体   English

在C ++ 11中结合使用std :: function和union

[英]Using std::function with union in C++11

I am trying to define a class that has functions with different numbers of parameters stored in a union. 我试图定义一个类,该类的函数具有存储在联合中的不同数量的参数。 The class is initialized with a function object and necessary parameters. 使用函数对象和必要的参数初始化该类。 I am getting these compiler errors at the locations marked (1) and (2) 我在标记为(1)和(2)的位置处收到这些编译器错误

(1) Destructor of '_' is implicitly deleted because variant field 'f_V' has a non-trivial destructor (1)隐式删除“ _”的析构函数,因为变量字段“ f_V”具有非平凡的析构函数

(2) Destructor of 'MyClass' is implicitly deleted because field 'functions' has a deleted destructor (2)因为字段“函数”具有已删除的析构函数,所以隐式删除了“ MyClass”的析构函数

I simply want to declare a union of different function objects, and depending on the parameters passed-in be able to pick the appropriate function and call it. 我只想声明不同函数对象的并集,并根据传入的参数选择合适的函数并对其进行调用。 Why am I getting these errors and how should I rewrite this code? 为什么会出现这些错误,我应该如何重写此代码?

template<typename VARIABLE> struct MyClass {
    MyClass(VARIABLE *p, const std::function<void (VARIABLE&)>& f) {
        functions.f_V = f;
        ...
    }
protected:
    union _ {
        std::function<void (VARIABLE &)> f_V; // (1)
        std::function<void (const VARIABLE &, VARIABLE &)> f_vV;
        std::function<void (const VARIABLE &, const VARIABLE &, VARIABLE &)> f_vvV;
        std::function<void (const VARIABLE &, const VARIABLE &, const VARIABLE &, VARIABLE &)> f_vvvV;
    } functions; // (2)
    ...
 };

EDIT: I am trying to accomplish storage of lambda functions having variable argument lists in a container. 编辑:我正在尝试完成容器中具有可变参数列表的lambda函数的存储。 Something like this: 像这样:

std::vector<MyClass<MyVariable>> myContainerOfLambdas;
MyVariable A,B,TO;
auto lambda1 = [this](const MyVariable& a, MyVariable& to) { ... };
myContainerOfLambdas.push_back(MyClass<MyVariable>(A,TO,lambda1));
auto lambda2 = [this](const MyVariable& a, const MyVariable& b, MyVariable& to) { ... };
myContainerOfLambdas.push_back(MyClass<MyVariable>(A,B,TO,lambda2));
...
// iterate over the stored MyClass objects holding the lamda functions and the parameters
// to call them
for(MyClass<MyVariable> & e:myContainerOfLambdas) {
  e(); // here the appropriate lambda function will be invoked with the appropriate values
  // stored in MyClass
}

NOTE: For brevity I have omitted the definition of the () operator in MyClass, but it simply calls the right function object with the right parameters. 注意:为简便起见,我省略了MyClass中的()运算符的定义,但它只是使用正确的参数调用正确的函数对象。

If you see a better design approach to this, fine, please direct me in the right direction. 如果您认为有更好的设计方法,可以,请以正确的方向指导我。 Thanks! 谢谢!

You can use the unrestricted unions feature of C++11 to do with you want. 您可以使用C ++ 11的不受限制的联合功能来完成所需的工作。 You will need to implement a custom constructor and destructor on the union that constructs/deletes the appropriate active member. 您将需要在构造/删除适当的活动成员的联合上实现自定义构造函数和析构函数。

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

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