lundi 15 février 2021

How to bind a function template with pybind11?

I want to bind a function template with pybind11 (A function template by itself is not a type, or a function, or any other entity. No code is generated from a source file that contains only template definitions.).

template<typename tree_iterator>
auto function(const tree_iterator first, const tree_iterator last) {

    tree_iterator ti, tj;
    auto ntrees = last - first;
 
    return ntrees;
 
}

How can I solve this issue?

When I execute python setup.py bdist_wheel --force_debug command I get this error:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/workspace/dragon/multiband-fusion/build/temp.linux-x86_64-3.8
Scanning dependencies of target mbfusion
[ 50%] Building CXX object src/CMakeFiles/mbfusion.dir/main.cpp.o
/home/user/workspace/dragon/multiband-fusion/src/main.cpp: In function ‘void hg::tree_fusion_internal::pybind11_init_mbfusion(pybind11::module_&)’:
/home/user/workspace/dragon/multiband-fusion/src/main.cpp:64:63: error: no matching function for call to ‘pybind11::module_::def(const char [9], <unresolved overloaded function type>, const char [21])’
   64 |             m.def("function", function, "An example function.");
      |                                                               ^
In file included from /home/user/workspace/dragon/multiband-fusion/src/main.cpp:1:
/home/user/anaconda3/envs/dragon/lib/python3.8/site-packages/higra/lib/include/pybind11/pybind11.h:911:14: note: candidate: ‘template<class Func, class ... Extra> pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...)’
  911 |     module_ &def(const char *name_, Func &&f, const Extra& ... extra) {
      |              ^~~
/home/user/anaconda3/envs/dragon/lib/python3.8/site-packages/higra/lib/include/pybind11/pybind11.h:911:14: note:   template argument deduction/substitution failed:
/home/user/workspace/dragon/multiband-fusion/src/main.cpp:64:63: note:   couldn’t deduce template parameter ‘Func’
   64 |             m.def("function", function, "An example function.");
      |                                                               ^
make[2]: *** [src/CMakeFiles/mbfusion.dir/build.make:82: src/CMakeFiles/mbfusion.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:134: src/CMakeFiles/mbfusion.dir/all] Error 2
make: *** [Makefile:114: all] Error 2
Traceback (most recent call last):
  File "setup.py", line 70, in <module>
    setup(
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/site-packages/wheel/bdist_wheel.py", line 299, in run
    self.run_command('build')
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "setup.py", line 41, in run
    self.build_extension(ext)
  File "setup.py", line 67, in build_extension
    subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
  File "/home/user/anaconda3/envs/dragon/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--config', 'Debug', '--', '-j2']' returned non-zero exit status 2.

Aucun commentaire:

Enregistrer un commentaire