将 cppyy 与右值指针和映射配合使用

Using cppyy with rvalue pointers and maps

提问人:shouriha 提问时间:6/14/2023 更新时间:6/14/2023 访问量:70

问:

我很想爱cppyy。但是,我使用的代码库大量使用std.unique_ptr、右值指针和模板。我对如何将这些翻译成我可以从 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;
};
""")

但是当我尝试时:

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"])

我收到一个很长的错误:

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

我做错了什么?

python c++ std python-bindings cppyy

评论


答:

4赞 Ted Lyngmo 6/14/2023 #1

您可以使用插入键/值对,也可以使用从中查找.emplace.at

例:

#!/bin/python

import cppyy
import cppyy.gbl as Cpp

# the class with an added operator<< overload to support printing:
cppyy.cppdef(r"""\
template<typename T>
class MyClass {
public:
    MyClass(T t) : m_data(t) {}
    T m_data;
    friend std::ostream& operator<<(std::ostream& os, const MyClass& mc) {
        return os << mc.m_data;
    }
};
""")

from cppyy.gbl import MyClass

test_map = Cpp.std.map[Cpp.std.string, MyClass['double']]()

myObj = MyClass['double'](5.0)

# add a key/value pair
test_map.emplace("key", Cpp.std.move(myObj))

# print the value mapped to the key:
print(test_map.at("key"))

# loop and print keys and values
for key, value in test_map:
    print(key, value)

输出:

5
key 5