使用 switch 语句将不同的函数作为类属性分配 C++
Assigning different functions as class attributes using a switch statement in C++
我正在使用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
。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- 使用 CTRP 时,是否访问访问父构造函数 UB 中的子属性?