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 8238669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:53:30+00:00 2026-06-07T19:53:30+00:00

I was somewhat surprised when I got this error message. TypeError: unsupported operand type(s)

  • 0

I was somewhat surprised when I got this error message.

TypeError: unsupported operand type(s) for <<: 'Packet' and 'int'

What I am trying to do is expose a class through boost python that overloads the __lshift__ operand to take several different object types – such as int, float, char*, etc. I am exposing the class correctly but when I try something like packet << 10 I get the aforementioned error.

I understand why this is but I’m not sure exactly how I should go about solving this. I cant exactly have an overload with a boost::python::object because then it couldn’t distinguish between packet << 10 and packet << 10.5.

One possible solution is to expose the functions as something other than the __lshift__. I could then end up with packet.saveInt(10) and packet.saveFloat(10.5) but that destroys the syntactic sugar that we all so admire in python.

Another idea I had was that I could create a very thin class around the built-in int types and take that in.

struct Int32 {
    Int32(bp::object obj) {
        if (PyInt_Check(obj.get()) {
            value = bp::extract<int>(obj);
        }
        PyErr_SetObject(PyExc_ValueError, obj);
        bp::throw_error_already_set();
    }
    int value;
}

BOOST_PYTHON_MODULE(intwrapper)
{
        bp::class_<Int32>("Int32", bp::init<bp::object>)
            .def("value", &Int32::value);
}

With this I could then do the following. packet << Int32(10) and similarly packet << Float(10.5). This is kind of hackish and I hope there is a better way.

Any suggestions? Thanks!

  • 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-06-07T19:53:32+00:00Added an answer on June 7, 2026 at 7:53 pm

    After more investigation I discovered the problem is that I was trying to pass an int by reference and Python does not support that.

    I did my best to keep the syntax sugar but eventually went with exposing the different operator <<() and operator >>() methods as saveInt32(value), saveString(value), loadInt32(), loadString() and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just got an error message that was somewhat unexpected: Error Message: Could not
Somewhat related to this question . I'm trying to create a window with two
Somewhat related to this question , but in the absence of any answer about
Somewhat similar to this question , except we haven't decided that we're going with
this is somewhat of an odd question. I wrote a C function. Its 'like'
I was somewhat surprised to find that std::shared_ptr provides no protection (eg, throw an
Somewhat confused by GA, I got to thinking .. surely I can track specific
Somewhat academic question, but: How do value types like Int actually work ? I've
I was somewhat surprised to see the below work coming from a C++ background.
Somewhat on the back of this question that asks about the behaviour of the

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.