I'm writing python bindings for glog like library which uses macro and has cout like syntax for logging.
LOG(LEVEL)<<" "<<" "...
.
So I'm using this function to call the macro
template <typename Arg, typename... Args>
void log(auto level, Arg&& arg, Args&&... args)
{
std::stringstream out;
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander{0, (void(out << ' ' << std::forward<Args>(args)), 0)...};
LOG(level) << out.str();
}
So in order to wrap this function for pybind11 module I need to explicitly specify template type. Is there any possible workaround or way to bind this function using pybind11? I'm also open to use other libraries like boost.python or even cython if its possible.
Mixing templates and Python is best done at run-time to make sure you have all template instantiations that will actually be used. You can do that with cppyy (http://cppyy.org), which uses Cling (LLVM) underneath to instantiate the templates.
Using your example (with
cerr
replacing yourLOG
, the specific code for which you did not post):which has the expected out of: