How to expose a c++ function taking variable arguments in boost python -
I have a C ++ function that takes the variable number of arguments.
Four Consists * Fun (int number, ...) {..... Some processing on the arguments given in}} Boost Python The code has been written to highlight this function,
Namespace Promotion: Using Python Tax; BOOST_PYTHON_MODULE (lib_boost) {DEF ("fun", fun); } This code gives the following error when compiling
/boost_1_42_0/boost/python/data_members.hpp15 In the file included, / from_1_42_0 / Boost / python / class.hpp.1, /boost_1_42_0/boost/python.hpp_1, from Lib_boost.h: 3, from Lib_boost.cpp: 1: /boost_1_42_0/boost/python/make_function.hpp: 'Boost in Function: : Python :: api :: object boost :: python :: make_function (f) [f with = const char * () (int, ...) ': / boost_1_42_0 / boost / python / def Hpp: 82:
'boost :: python :: api :: object boost :: Python :: extension :: make_function1 (t, ...) [with t = const char instantiated (< Em>) (int, ...) '/boost_1_42_0/boost/python/def.hpp:91:' Zero Bust :: Python :: Def (Cons Charge) , FN) [FN = Const char * () (int, ...)] 'Lib_boost.cpp: 540: From here instant / boost_1_42_0 / boost / python / make_function .hpp: 104: Error: Invalid conversion from const char to () (int, ...) 'to' const char () (int) / boost_1_42_0 / boost / Python / make_function .hpp: 104: Error:
To begin the argument, 'Boost :: Emple :: vector2 boost :: Python :: detail :: get_signature (RT () (T), Zero *) [My understanding from the above error information with RT could not recognize the function by taking boot python variable arguments (illegal conversion from 'const char * () (Int, ...) 'const char (*) (int)') To take a function to highlight a function with the set / known set of arguments How to highlight a function with variable arguments?
If you are using c ++ 11, then you can do the following (G + + Based on - 4.8.2)
#include & lt; Boost / python.hpp & gt; # Include & lt; Boost / python / list.hpp & gt; #include & lt; Vector & gt; #include & lt; String & gt; # Include & lt; Cstdarg & gt; # Include & lt; Cassert & gt; Namespace Promotion: Using Python; Template & lt; Int ... index & gt; Structure index {next = index and lieutenant; Indices ..., sizes (indices)>; }; Template & lt; Int N & gt; Struct build_indices {typing ty type = build_indice & lt; N-1 & gt; :: type :: next; }; Template & lt; & Gt; Struct build_indices & lt; 0 & gt; {Using type = indices & lt; & Gt ;; }; Template & lt; Int N & gt; BuildIndices = typename build_indices & lt; N & gt; :: using typing; Template & lt; Int num_args & gt; Class Unpack_keller {Private: Template & lt; Typename FuncType, int ... I & gt; Four * Calls (Functype & F, Stade :: Vector & lt; four * & gt; & amp; Eggs; Index; & lt; i ... & gt;) {Returns F (args.size), RGS ] ...); } Public: Template & lt; Typename FuncType & gt; Four * operator () (function and f, study :: vector & lt; four * & gt; & amp; egags) {emphasis (args.size () Vec; (Int index = 1; index and lieutenant; = count; ++ index) for {extract & lt; Four *> Str (L [index]); If (str.check ()) {// Remove items from the list and create vector vec.push_back (str ()); }} // Maximum of 20 arguments will be processed. Unpack_caller's & lt; 20> Callers; Return std :: string (collar (my_func, vec)); } Return std :: string (""); } BOOST_PYTHON_MODULE (my_module) {def ("my_func", my_func_overload); } In Python:
Python 2.7.6 (default, 22 March 2014, 22:59:38) [GCC 4.8.2] at linux2 For more information, type "help", "copyright", "credit" or "license" & gt; & Gt; & Gt; Import my_module M & gt; & Gt; & Gt; M.my_func ([5, "my", "first", "five", "string", "argument"]) 'my first five string arguments' & gt; & Gt; & Gt; In this example, "char * my_func (int a, ...)" just connects all string arguments and returns the resulting string.
Comments
Post a Comment