Skip to content

[Bug] soxr-1.0.0 won't complie #434

@randynovak

Description

@randynovak

Version

2.0.0 (Default)

System Information

Python 3.10.18
soxr-1.0.0.
gcc version 7.3.1 20180712 (RedHat 7.3.1-17) (GCC)
Target: x86_64-redhat-linux

Description

The Problem

The core issue was with how C++ template functions were being bound to Python using nanobind. The original code was trying to directly bind template functions like this:

.def("process_float32", &CSoxr::process)

This approach doesn't work well with nanobind because:

  1. C++ template functions need to be explicitly instantiated for binding
  2. The compiler couldn't deduce the template parameters when binding directly
  3. Template specializations can't be directly used as function pointers in this context

How to Reproduce the Bug

The problem persists until solved, below.

The Solution

Replaced all direct template function bindings with lambda functions that explicitly call the template functions with the correct types:
.def("process_float32", [](CSoxr& self, ndarray<const float, nb:ndim<2>, nb::c_contig, nb::device::cpu> input, bool last=false) {
return self.process(input, last);
})

This approach works because:

  1. The lambda function is not a template - it's a concrete function that nanobind can bind
  2. Inside the lambda, we explicitly call the template function with the specific type
  3. The lamba's parameter types exactly match what the template function expects
  4. This pattern provides a "bridge" between Python and the C++ template functions

Applied this pattern consistently to all template functions in the binding code, ensuring that each one is properly instantiated with its specific type.
This is a common pattern when binding C++ template functions to Python, as most binding libraries (including nanobind, pybind11, etc.) require explicit instantiation of templates.

Metadata

Metadata

Assignees

Labels

type: bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions