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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:59:51+00:00 2026-06-06T14:59:51+00:00

I have the following class: #include <array> template<unsigned short D> class Point { private:

  • 0

I have the following class:

#include <array>

template<unsigned short D>
class Point {
private:
    std::array<float, D> coordinates;
public:
    Point() { for(int i=D-1; i>=0; --i) coordinates[i] = 0.0; }
    Point(const Point& rhs) = default;
    Point& operator=(const Point& rhs) = default;
    ~Point() = default;

    float& get_ref(const unsigned short dimension)
        { return coordinates[dimension-1]; }
};

I’m trying to wrap it with:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(fernpy) {
    using namespace boost::python;

    class_< Point<2> >("point")
        .def("__call__", &Point<2>::get_ref, return_internal_reference<>());
}

I’m using gcc-4.7 to compile for boost 1.48, python-2.7 on Fedora 17. All the code is a file called testpy.cpp. I’m using these commands to compile:

g++ -std=c++11 -g -fPIC -I/usr/include/python2.7 -c testpy.cpp
g++ -shared -g -lpython2.7 -lboost_python -o libfern.so testpy.o

The compiler returns a bunch of boost internal errors, too many to post here. This excerpt seems to be the core of it. There are a bunch of “required from”s before it and “note”s after.

/usr/include/boost/python/object/make_instance.hpp:27:9: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::or_<boost::is_class<float>, boost::is_union<float>, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >::************)’

It works just fine if I return a plain float from get_ref and remove the return_internal_reference<>() argument from the .def line of the wrapper. It’s weird because I’m doing the same thing with another, more complicated class template and it works just fine there too. I’ve been Googling and banging my head against this for almost a full day now. Anybody have any idea what the heck is going on?

UPDATE:

I ended up using the “getitem” and “setitem” special methods from python, a la this link. The link shows how to define a nifty struct template with static wrappers for access functions, so you don’t have to mess with the interface to your original C++ class.

  • 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-06T14:59:52+00:00Added an answer on June 6, 2026 at 2:59 pm

    From a python point-of-view, floats are an immutable-type. As such, python does not allow changing the value.

    For example, the following occurs in python:

    coordinates = [ 5, 10, 15 ]
    x = cooardinates[ 2 ] # Bind x to refer to the int(15) object.
    x = 5                 # Rebind x to refer to the int(5) object. 
                          # Does not modify coordinates.
    

    Now, consider the following:

    from fernpy import point
    p = point()
    x = p(2) # Bind x to refer to the float(p(2)) object.
    x = 5    # Rebind x to refer to the int(5) object.
             # Does not set p.coordinates[2] to 5.
    

    Thus, boost::python prevents returning reference to types that will be immutable in python because Python does not support it. x does not store the value 5; instead, it contains a reference to the 5 object. If assigning to x did not rebind x, then nonsensical statements, such as 6 = 5 would be possible.

    The compile error is a static check that limits return_internal_reference to only work with classes or unions, as those will be mutable types within Python. I imagine that the ‘more complicated class template’ that works is returning a reference to a user-type.

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

Sidebar

Related Questions

I have the following structure: template <class T> struct Array{ int lenght; T *
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following code compiled by gcc: #include <iostream> using namespace std; class
If I have the following basic C++ program: #include <iostream> using namespace std; class
I have the following class: abstract class TheView { public $template = NULL; public
Lets say we have the following two class definitions. #include <iostream> #include <array> class
I have the following abilities model: class Ability include CanCan::Ability def initialize(user) user ||=
Lets say I have the following DataMapper resources: class Post include DataMapper::Resource has n,
I currently have the following models/toy.rb file in my RoR project: class ToysPurchased include

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.