@@ -193,6 +193,20 @@ class MyObject5 { // managed by huge_unique_ptr
193193 int value;
194194};
195195
196+ // test const_only_shared_ptr
197+ class MyObject6 {
198+ public:
199+ static const_only_shared_ptr<MyObject6> createObject (std::string value) {
200+ return const_only_shared_ptr<MyObject6>(new MyObject6 (std::move (value)));
201+ }
202+
203+ const std::string &value () const { return value_; }
204+
205+ private:
206+ explicit MyObject6 (std::string &&value) : value_{std::move (value)} {}
207+ std::string value_;
208+ };
209+
196210// test_shared_ptr_and_references
197211struct SharedPtrRef {
198212 struct A {
@@ -412,11 +426,6 @@ TEST_SUBMODULE(smart_ptr, m) {
412426 m.def (" print_myobject2_4" ,
413427 [](const std::shared_ptr<MyObject2> *obj) { py::print ((*obj)->toString ()); });
414428
415- m.def (" make_myobject2_3" ,
416- [](int val) { return const_only_shared_ptr<MyObject2>(new MyObject2 (val)); });
417- m.def (" print_myobject2_5" ,
418- [](const const_only_shared_ptr<MyObject2> &obj) { py::print (obj.get ()->toString ()); });
419-
420429 py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, " MyObject3" ).def (py::init<int >());
421430 m.def (" make_myobject3_1" , []() { return new MyObject3 (8 ); });
422431 m.def (" make_myobject3_2" , []() { return std::make_shared<MyObject3>(9 ); });
@@ -459,6 +468,10 @@ TEST_SUBMODULE(smart_ptr, m) {
459468 .def (py::init<int >())
460469 .def_readwrite (" value" , &MyObject5::value);
461470
471+ py::class_<MyObject6, const_only_shared_ptr<MyObject6>>(m, " MyObject6" )
472+ .def (py::init ([](const std::string &value) { return MyObject6::createObject (value); }))
473+ .def_property_readonly (" value" , &MyObject6::value);
474+
462475 // test_shared_ptr_and_references
463476 using A = SharedPtrRef::A;
464477 py::class_<A, std::shared_ptr<A>>(m, " A" );
0 commit comments