提问人:shouriha 提问时间:6/14/2023 更新时间:6/14/2023 访问量:29
子类化或模板化对象的 Cppyy 映射
Cppyy map of subclassed or templated objects
问:
作为将 cppyy 与右值指针和映射一起使用的后续问题(很抱歉没有想出一个更简单的例子)
我试图从 初始化 .我不确定如何构造一个对象,该对象接受模板化自定义类的映射,因为似乎以相同的方式对待指针和对象。NameToBasisModel(std::map<std::string, std::unique_ptr<Basis<TBasisTemplateType>>>&& nameToBasisMap)
cppyy
unique_ptrs
cppyy
在 C++ 代码中,调用如下所示:
// use the PSDs from the output of LALInferenceReadData to fill the FixedNoiseModelComponent
std::map<std::string, std::unique_ptr<Basis<PowerSpectrumDensity>>> nameToBasisMap;
for (const auto& [interferometerName, interferometer] : data_->getInterferometerNameToInterferometerMap()) {
PowerSpectrumDensity powerSpectrumDensity{interferometer.powerSpectrumDensity.value()};
const std::string identifier = "fixed noise basis for " + interferometerName;
auto basis = std::make_unique<FixedBasis<PowerSpectrumDensity>>(identifier, std::move(powerSpectrumDensity),
PowerSpectrumDensity{powerSpectrumDensity.getSegmentLengthSec(), powerSpectrumDensity.getSamplingRateHz()});
nameToBasisMap.emplace(interferometerName, std::move(basis));
}
auto noiseModel = std::make_unique<NameToBasisModel<PowerSpectrumDensity>>(std::move(nameToBasisMap));
为了制作我的地图,我首先尝试了:
# make noise model
nameToBasisMap = Cpp.std.map[Cpp.std.string, Cpp.Basis[Cpp.PowerSpectrumDensity]]()
for interferometerName, interferometer in self.data.getInterferometerNameToInterferometerMap():
print(interferometerName, interferometer)
powerSpectrumDensity = Cpp.PowerSpectrumDensity(interferometer.powerSpectrumDensity.value())
identifier = "fixed noise basis for " + interferometerName
basis = Cpp.FixedBasis[Cpp.PowerSpectrumDensity](
identifier,
Cpp.std.move(powerSpectrumDensity),
Cpp.std.move(Cpp.PowerSpectrumDensity(powerSpectrumDensity.getSegmentLengthSec(),
powerSpectrumDensity.getSamplingRateHz()))
)
nameToBasisMap.emplace(interferometerName, Cpp.std.move(basis))
然而,这在制作地图时失败了(这并不奇怪,Basis本身没有实现。
(这是长回溯的一部分,但在尝试调用 emplace 时失败)
map<std::string,Basis<PowerSpectrumDensity> >::map<std::string,Basis<PowerSpectrumDensity> >(const std::map<std::string,Basis<PowerSpectrumDensity> >::allocator_type& __a) =>
TypeError: takes at least 1 arguments (0 given)
map<std::string,Basis<PowerSpectrumDensity> >::map<std::string,Basis<PowerSpectrumDensity> >(const std::map<std::string,Basis<PowerSpectrumDensity> >& __m, const std::map<std::string,Basis<PowerSpectrumDensity> >::allocator_type& __a) =>
TypeError: takes at least 2 arguments (0 given)
std::map<std::string,Basis<PowerSpectrumDensity> > constructor failed
map<std::string,Basis<PowerSpectrumDensity> >::map<std::string,Basis<PowerSpectrumDensity> >(const std::map<std::string,Basis<PowerSpectrumDensity> >& __m) =>
TypeError: takes at least 1 arguments (0 given)
Failed to instantiate "map<std::string,Basis<PowerSpectrumDensity> >()"
然后我尝试用子类替换nameToBasisMap = Cpp.std.map[Cpp.std.string, Cpp.Basis[Cpp.PowerSpectrumDensity]]()
nameToBasisMap = Cpp.std.map[Cpp.std.string, Cpp.FixedBasis[Cpp.PowerSpectrumDensity]]()
这毫无怨言地创建了地图。但是,当我尝试构建NameToBasisModel
noiseModel = Cpp.NameToBasisModel[Cpp.PowerSpectrumDensity](Cpp.std.move(nameToBasisMap))
它出现段错误。
我也试图用
nameToBasisMap = Cpp.std.map[Cpp.std.string, Cpp.std.unique_ptr[Cpp.Basis[Cpp.PowerSpectrumDensity]]]()
这也崩溃了。
我做错了什么?如何构造在类型中显式使用指针的东西?
答: 暂无答案
评论