Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:51:34+00:00 2026-05-22T23:51:34+00:00

I have my own string class and I want to export it into python

  • 0

I have my own string class and I want to export it into python (with boost.python) and use as a native string. I wrote converters for this.

The first is exporting of my string:

bp::class_<CL_StringRef8>("CL_StringRef8", bp::init<const std::string&>())
    .def("CStr", &CL_StringRef8::c_str);

Converters are from default tutorial but with my type:

// CL_StringRef8 → Python string --------------------------------------
struct cl_stringref8_to_python_str
{
    static PyObject* convert(CL_StringRef8 const& s)
    {
        return boost::python::incref(boost::python::object(s.c_str()).ptr());
    }
};

// Python string → CL_StringRef8 --------------------------------------
struct cl_stringref8_from_python_str
{
    cl_stringref8_from_python_str()
    {
        bp::converter::registry::push_back(
            &convertible,
            &construct,
            boost::python::type_id<CL_StringRef8>()
        );
    }

    static void* convertible(PyObject* obj_ptr)
    {
        if (!PyString_Check(obj_ptr)) return 0;
        return obj_ptr;
    }

    static void construct(PyObject* obj_ptr, bp::converter::rvalue_from_python_stage1_data* data)
    {
        const char* value = PyString_AsString(obj_ptr);
        if (value == 0) bp::throw_error_already_set();
        void* storage = ((bp::converter::rvalue_from_python_storage<CL_StringRef8>*)data)->storage.bytes;
        new (storage) CL_StringRef8(value);
        data->convertible = storage;
    }
};

Now, as I understand, I can call c++ functions from python which takes in arguments CL_StringRef8, but I have a code:

print GetMyStringObject()
data = float(GetMyStringObject())

# ==>

<CL_StringRef object at 0x7fd15dfd9de8>
TypeError: float() argument must be a string or a number

As I understand I have to export str() method for the CL_StringRef8 but I can’t do that:

bp::class_<CL_StringRef8>("CL_StringRef8", bp::init<const std::string&>())
    .def("CStr", &CL_StringRef8::c_str)
    .def(bp::self_ns::str(bp::self_ns::self)); 

Last line calls an error:

/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream<Target, Source, Traits>::operator<<(const Source&) [with Target = std::basic_string<char>, Source = CL_StringRef8, Traits = std::char_traits<char>]’:
/usr/include/boost/lexical_cast.hpp:1151:13:   instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, std::size_t) [with Target = std::basic_string<char>, Source = CL_StringRef8, bool Unlimited = true, CharT = char, typename boost::call_traits<Source>::param_type = const CL_StringRef8&, std::size_t = long unsigned int]’
/usr/include/boost/lexical_cast.hpp:1174:77:   instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char>, Source = CL_StringRef8]’
/usr/include/boost/python/operators.hpp:357:1:   instantiated from ‘static PyObject* boost::python::detail::operator_1<(boost::python::detail::operator_id)19u>::apply<T>::execute(boost::python::detail::operator_1<(boost::python::detail::operator_id)19u>::apply<T>::self_t&) [with T = CL_StringRef8, PyObject = _object, boost::python::detail::operator_1<(boost::python::detail::operator_id)19u>::apply<T>::self_t = CL_StringRef8]’
/usr/include/boost/python/operators.hpp:152:11:   instantiated from ‘void boost::python::detail::operator_<id, L, R>::visit(ClassT&) const [with ClassT = boost::python::class_<CL_StringRef8>, boost::python::detail::operator_id id = (boost::python::detail::operator_id)19u, L = boost::python::detail::not_specified, R = boost::python::detail::not_specified]’
/usr/include/boost/python/def_visitor.hpp:31:9:   instantiated from ‘static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor<boost::python::detail::operator_<(boost::python::detail::operator_id)19u> >, classT = boost::python::class_<CL_StringRef8>]’
/usr/include/boost/python/def_visitor.hpp:67:9:   instantiated from ‘void boost::python::def_visitor<DerivedVisitor>::visit(classT&) const [with classT = boost::python::class_<CL_StringRef8>, DerivedVisitor = boost::python::detail::operator_<(boost::python::detail::operator_id)19u>]’
/usr/include/boost/python/class.hpp:225:9:   instantiated from ‘boost::python::class_<T, X1, X2, X3>::self& boost::python::class_<T, X1, X2, X3>::def(const boost::python::def_visitor<Derived>&) [with Derived = boost::python::detail::operator_<(boost::python::detail::operator_id)19u>, W = CL_StringRef8, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified, boost::python::class_<T, X1, X2, X3>::self = boost::python::class_<CL_StringRef8>]’
/home/ockonal/Workspace/Themisto/src/Scripts/Core/TypesConverters.cpp:375:49:   instantiated from here
/usr/include/boost/lexical_cast.hpp:595:48: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.0/../../../../include/c++/4.6.0/ostream:581:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = CL_StringRef8]’

ps and yeah, I didn’t forget to register my converters into python.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:51:34+00:00Added an answer on May 22, 2026 at 11:51 pm

    In order for the .def(bp::self_ns::str(bp::self_ns::self)); line to work, you need to have the streaming operator defined for your class.

    std::ostream & operator<<(std::ostream & os, const CL_StringRef8 &s)
    { os << s.c_str(); return os; } 
    

    An alternative way of defining the str() method without needing the streaming operator is:

    .def("__str__", &CL_StringRef8::c_str) 
    

    Although, I prefer to just do the repr() method instead which is then used by str() when needed

    .def("__repr__", &CL_StringRef8::c_str)  
    

    All this still won’t make float() work in Python, since it expects either a string (not an object, even if it supports __str__), a float, or an object that supports __float__.

    We can do the latter though, by binding a lexical_cast around your c_str() method:

    #include <boost/lexical_cast.hpp>
    #include <boost/mpl/vector.hpp>  
    

    and then

    .def("__float__", bp::make_function( 
                        boost::bind(&boost::lexical_cast<float,CL_StringRef8>,_1),
                          bp::default_call_policies(), 
                            boost::mpl::vector<float,CL_StringRef8>()));
    

    Now you can do this in Python:

    >>> a = CL_StringRef8("1.234")
    >>> print a
    1.234
    >>> float(a) * 2
    2.468
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building my own String class and I want to write my own replace
I am creating a simple app for my own use. I want to have
I'm trying to hide window after its startup. I have own window-class which is
We have our own ORM we use here, and provide strongly typed wrappers for
I have my own asp.net cookie created like this: var authTicket = new FormsAuthenticationTicket(
I'm trying to write my own C++ String class for educational and need purposes.
i want to use string inside Union. if i write as below union U
in eclipse plugin i have two plug-in which are used there own class loader
Say if I have a class in java and I want it to be
I have something like this public class Reminders() { public List<SelectListItem> Reminder { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.