我会很喜欢cppy的.但是,我使用的代码库大量使用了std.only_ptr、rValue指针和模板.我搞不懂如何将这些转换成我可以从Python调用的东西.
例如,我被困在如何从类创建std::map上.
我知道我可以通过做以下几件事得到std::map
分:
test_map = Cpp.std.map[Cpp.std.string, Cpp.std.string]()
test_string = "value"
test_map["key"] = test_string
print(test_map["key"])
然而,当我这样做的时候:
test_map = Cpp.std.map[Cpp.std.string, Cpp.std.string]()
test_string = Cpp.std.string("value")
test_map["key"] = Cpp.std.move(test_string)
print(test_map["key"])
我得到
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 3
1 test_map = Cpp.std.map[Cpp.std.string, Cpp.std.string]()
2 test_string = Cpp.std.string("value")
----> 3 test_map["key"] = Cpp.std.move(test_string)
4 print(test_map["key"])
TypeError: none of the 2 overloaded methods succeeded. Full details:
std::string& std::map<std::string,std::string>::operator[](std::map<std::string,std::string>::key_type&& __k) =>
logic_error: basic_string::_M_construct null not valid
std::string& std::map<std::string,std::string>::operator[](const std::map<std::string,std::string>::key_type& __k) =>
logic_error: basic_string::_M_construct null not valid
我不确定这为什么会失败.
我实际上想要构造的是从字符串到模板化类的映射,请参见:
import cppyy
import cppyy.gbl as Cpp
cppyy.cppdef(r"""\
template<typename T>
class MyClass {
public:
MyClass(T t) : m_data(t) {}
T m_data;
};
""")
但当我try 的时候:
test_map = Cpp.std.map[Cpp.std.string, Cpp.MyClass['double']]()
myClass = Cpp.MyClass['double'](5.0)
test_map["key"] = Cpp.std.move(myClass)
print(test_map["key"])
我得到 a long error:
input_line_50:6:86: error: no member named 'operator[]' in 'std::map<std::__cxx11::basic_string<char>, MyClass<double>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, MyClass<double> > > >'
new (ret) (MyClass<double>*) (&((std::map<std::string,MyClass<double> >*)obj)->operator[]((std::string&&)*(std::string*)args[0]));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
input_line_50:10:55: error: no member named 'operator[]' in 'std::map<std::__cxx11::basic_string<char>, MyClass<double>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, MyClass<double> > > >'
((std::map<std::string,MyClass<double> >*)obj)->operator[]((std::string&&)*(std::string*)args[0]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
input_line_51:6:86: error: no member named 'operator[]' in 'std::map<std::__cxx11::basic_string<char>, MyClass<double>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, MyClass<double> > > >'
new (ret) (MyClass<double>*) (&((std::map<std::string,MyClass<double> >*)obj)->operator[]((const std::string&)*(const std::string*)args[0]));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
input_line_51:10:55: error: no member named 'operator[]' in 'std::map<std::__cxx11::basic_string<char>, MyClass<double>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, MyClass<double> > > >'
((std::map<std::string,MyClass<double> >*)obj)->operator[]((const std::string&)*(const std::string*)args[0]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[45], line 3
1 test_map = Cpp.std.map[Cpp.std.string, Cpp.MyClass['double']]()
2 myClass = Cpp.MyClass['double'](5.0)
----> 3 test_map["key"] = Cpp.std.move(myClass)
4 print(test_map["key"])
TypeError: none of the 2 overloaded methods succeeded. Full details:
MyClass<double>& std::map<std::string,MyClass<double> >::operator[](std::map<std::string,MyClass<double> >::key_type&& __k) =>
ReferenceError: none of the 2 overloaded methods succeeded. Full details:
attempt to access a null-pointer
attempt to access a null-pointer
MyClass<double>& std::map<std::string,MyClass<double> >::operator[](const std::map<std::string,MyClass<double> >::key_type& __k) =>
TypeError: none of the 2 overloaded methods succeeded. Full details:
MyClass<double>& MyClass<double>::operator=(MyClass<double>&&) =>
ValueError: could not convert argument 1 (object is not an rvalue)
attempt to access a null-pointer
我做错了什么?