使用 switch 语句将不同的函数作为类属性分配 C++

Assigning different functions as class attributes using a switch statement in C++

本文关键字:属性 C++ 分配 函数 语句 switch 使用      更新时间:2023-10-16

我正在使用pybind11为python编写一个C++ Eigen扩展。

我希望其中一个扩展类采用一个参数,该参数指定属性要使用的几个函数中的哪一个。我的部分问题是我有限的C++经验意味着我发现很难阐明这个问题,所以我不胜感激任何关于术语的建议。

Python中,I Want to do 的简化版本如下所示:

class My_class:
def __init__(self, arg1, option):
self.arg1 = arg1
if option == 'option1'
self.operation = operation1
else:
self.operation = operation2
def my_method(self, arg):
return self.operation(arg, self.arg1)

我已经尝试遵循这个问题中公认的答案,并有一个将函数作为属性的类,尽管它没有按预期工作(如下所述(。

我尽我所能构建了一个最小的例子:

#define _USE_MATH_DEFINES
#include "kernels.h"
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <Eigen/LU>
#include <Eigen/Dense>
#include <math.h>
#include <unsupported/Eigen/SpecialFunctions>
// ================ Functions
Eigen::MatrixXd operation1(Eigen::Ref<const Eigen::MatrixXd> A, Eigen::Ref<const Eigen::MatrixXd> B){
return A.array() + B.array();
}
Eigen::MatrixXd operation2(Eigen::Ref<const Eigen::MatrixXd> A, Eigen::Ref<const Eigen::MatrixXd> B){
return A.array() * B.array();
}
// ================ Class
class Test
{
private:
Eigen::MatrixXd A;
std::function<Eigen::MatrixXd(Eigen::Ref<const Eigen::MatrixXd>,Eigen::Ref<const Eigen::MatrixXd>)> op;
public:
Test(const Eigen::MatrixXd &xdata, int k);
Eigen::MatrixXd operation(Eigen::Ref<const Eigen::MatrixXd>);
};
// class-constructor
Test::Test(const Eigen::MatrixXd &xdata, int k):A(xdata)
{
switch(k){
case 1:
op = operation1;
case 2:
op = operation2;
}
}
Eigen::MatrixXd Test::operation(Eigen::Ref<const Eigen::MatrixXd> B){
return Test::op(Test::A, B);
}
// ================ pybind11
namespace py = pybind11;
PYBIND11_MODULE(test5,m)
{
m.doc() = "pybind11 example plugin";
py::class_<Test>(m, "Test")
.def(py::init<Eigen::MatrixXd, int>())
.def("operation", &Test::operation)
.def("__repr__",[](const Test &a){return "<example.Test>";}
);
}

不希望的行为是 Test::op 只绑定到 operation2,即在 python 中调用operation方法只会将数组相乘,而不管该类是用k=1还是k=2初始化的。

编译扩展后,我在python中对其进行了如下测试:

from test5 import Test
import numpy as np
A = np.random.random((5,5))
B = np.random.random((5,5))
T = Test(A, 1)
np.allclose(T.operation(B), A + B) # Returns False
T = Test(A, 1)
np.allclose(T.operation(B), A * B) # Returns True
T = Test(A, 2)
np.allclose(T.operation(B), A * B) # Returns True

问题

  • 这个开关语句有什么问题?

  • 使用 switch 语句来实现此目的是否明智?我是否会因此而放弃 Test::op 中的任何编译器优化?

您在case语句后缺少一个break