Skip to content

boost::optional example doesn't work on older Boost #847

Closed
@bmerry

Description

@bmerry

Issue description

The documentation describes how to wrap boost::python. It fails to compile on Boost 1.58 due to the line value = {}; which was only fixed to work in Boost 1.63.

Reproducible example code

C++:

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

#define USE_BOOST
#ifdef USE_BOOST
#include <boost/optional.hpp>
using boost::optional;

namespace pybind11 { namespace detail {
    template <typename T>
    struct type_caster<boost::optional<T>> : optional_caster<boost::optional<T>> {};
}}
#else
#include <experimental/optional>
using std::experimental::optional;
#endif

static void func(optional<py::array_t<double>> x)
{
    if (x)
        std::cout << "Called with array of size " << x->size() << '\n';
    else
        std::cout << "Called with None\n";
}

PYBIND11_PLUGIN(use_opt)
{
    py::module m("use_opt", "Test wrapping of optional");
    m.def("func", &func);
    return m.ptr();
}

Python:

#!/usr/bin/env python
import numpy as np
import use_opt

use_opt.func(np.array([1.0, 2.0]))
use_opt.func(None)

There has been some discussion in gitter about approaches to fix this. I'm going to assign to myself to create a PR.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions