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

  • Home
  • SEARCH
  • 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 494463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:29:56+00:00 2026-05-13T05:29:56+00:00

I have a C++ value type wrapped with Boost.Python which has a concept of

  • 0

I have a C++ value type wrapped with Boost.Python which has a concept of a NULL value. The relevant parts of the wrapper code appear as follows:

class_<TCurrency> currency( "TCurrency" )
    .def( init<long>() )
    .def( init<const std::string&>() )
    <...>;

Currently, trying to create a NULL instance in Python by passing None to the __init__() method causes the C++ ctor accepting a const string reference to be called with an invalid reference. (&arg == NULL)

Is it possible to trap the case where None is being passed to a constructor and handle it gracefully or at least to throw a meaningful exception before my program crashes?

Using Boost 1.36 and Python 2.6.2.

  • 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-13T05:29:56+00:00Added an answer on May 13, 2026 at 5:29 am

    Adding an init<void*> overload will pass NULL if None is used, but I’m not sure how this could affect other ctors in corner cases. I also don’t get the same None to string const& conversion that you mention, if I leave init<void*> out. Using Boost.Python 1.37 and Python 2.6.2.

    Example:

    #include <iostream>
    #include <string>
    
    #include <boost/python.hpp>
    
    
    struct A {
    #define BODY { std::cout << __PRETTY_FUNCTION__ << '\n'; }
        A() BODY
        A(long) BODY
        A(std::string const&) BODY
        A(void* p) BODY
    #undef BODY
    };
    
    BOOST_PYTHON_MODULE(ex) {
    using namespace boost::python;
    class_<A>("A")
        .def(init<long>())
        .def(init<std::string const&>())
        .def(init<void*>())
    ;
    }
    
    >>> import ex
    >>> ex.A()
    A::A()
    <ex.A object at 0x839bf7c>
    >>> ex.A(42)
    A::A(long int)
    <ex.A object at 0x839bfcc>
    >>> ex.A("abc")
    A::A(const std::string&)
    <ex.A object at 0x839bf7c>
    >>> ex.A(None)
    A::A(void*)
    <ex.A object at 0x839bfcc>
    

    If init<void*> is left out:

    >>> ex.A(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    Boost.Python.ArgumentError: Python argument types in
        A.__init__(A, NoneType)
    did not match C++ signature:
        __init__(_object*, std::string)
        __init__(_object*, long)
        __init__(_object*)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Upon closer inspection of the CTCP rfc, I solved it… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer This code snippet should help you out, as long as… May 13, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer By default, Hibernate doesn't use dynamic updates and indeed updates… May 13, 2026 at 10:16 pm

Related Questions

I have a class that is wrapped with swig, and registered with lua. I
I am wrapping a native C++ class, which has the following methods: class Native
I have a very painful library which, at the moment, is accepting a C#
Sometimes it makes sense to have a key-ordered dictionary. In C++, this is often

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.