【问题标题】:Wrapping variadic templates in pybind11在 pybind11 中包装可变参数模板
【发布时间】:2023-03-16 23:50:01
【问题描述】:

我正在为类似 glog 的库编写 python 绑定,它使用宏并具有类似 cout 的日志记录语法。 LOG(LEVEL)<<" "<<" "...。 所以我用这个函数来调用宏

    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();
    }

所以为了为 pybind11 模块包装这个函数,我需要明确指定模板类型。 是否有任何可能的解决方法或方法来使用 pybind11 绑定此功能?如果可能的话,我也愿意使用其他库,如 boost.python 甚至 cython。

【问题讨论】:

    标签: c++ c++11 boost-python pybind11 pybinding


    【解决方案1】:

    最好在运行时混合模板和 Python,以确保您拥有所有将实际使用的模板实例。您可以使用 cppyy (http://cppyy.org) 来做到这一点,它使用下面的 Cling (LLVM) 来实例化模板。

    使用您的示例(用cerr 替换您的LOG,您没有发布的具体代码):

    import cppyy
    
    # variadic template example; the code can also live in a header which is loaded 
    # with "cppyy.include()"
    cppyy.cppdef(r"""
        template <typename Arg, typename... Args>
        void log(int 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)...};
            std::cerr << out.str() << '\n';
        }
    """)
    
    level = 1
    cppyy.gbl.log(level, 1, "string", 3.14)
    

    其中有预期的出:

    1 string 3.14
    

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多