type-traits handling and specialization

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

type-traits handling and specialization

Viktor Gal
Hi

based on the documentation of swig 3.0 "7.3.9 Type traits for metaprogramming” swig suppose to be able to handle type traits.
i have the following templated function my c++ code:

template <typename T>
inline bool append_element(typename std::enable_if<!std::is_base_of<CSGObject, std::remove_pointer<T>>::value, T>::type e, const char* name=“”);

and added the following %template directives into .i

%template(append_element_real) append_element<double>;
%template(append_element_float) append_element<float>;

when i try to call from target language append_element_float, i’m getting
NotImplementedError: Wrong number or type of arguments for overloaded function ‘append_element_float’

Possible C/C++ prototypes are:
    append_element< float32_t >(std::enable_if< !std::is_base_of<CSGObject,std::remove_pointer< float > >::value,float >::type,char const *)

any ideas?

or as i look into the code itself it’s just not possible to have this with swig support?

cheers,
viktor
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: type-traits handling and specialization

William S Fulton


On 9 July 2017 at 11:37, Viktor Gal <[hidden email]> wrote:
Hi

based on the documentation of swig 3.0 "7.3.9 Type traits for metaprogramming” swig suppose to be able to handle type traits.
i have the following templated function my c++ code:

template <typename T>
inline bool append_element(typename std::enable_if<!std::is_base_of<CSGObject, std::remove_pointer<T>>::value, T>::type e, const char* name=“”);

and added the following %template directives into .i

%template(append_element_real) append_element<double>;
%template(append_element_float) append_element<float>;

when i try to call from target language append_element_float, i’m getting
NotImplementedError: Wrong number or type of arguments for overloaded function ‘append_element_float’

Possible C/C++ prototypes are:
    append_element< float32_t >(std::enable_if< !std::is_base_of<CSGObject,std::remove_pointer< float > >::value,float >::type,char const *)

any ideas?

or as i look into the code itself it’s just not possible to have this with swig support?

You are missing a number of %template statements. Template are not automatically instantiated by SWIG, so you need a %template for each template being used, so in your case you need to also add one for each of std::enable_if, std::is_base_of, std::remove_pointer. SWIG does not provide a library interface file for the templates in the C++ header <type_traits>, like it does for those in <vector>, <list> etc. You can of course create your own interface file with the definitions of the templates that are necessary for SWIG to work. It would be nice if this were contributed back to the project for other SWIG users as we accept pull requests for enhancing the SWIG library with types in the C++ standard. They would need to go into a new std_type_traits.i file.

William


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: type-traits handling and specialization

Viktor Gal
Hi William,

thanks so much for getting back so quickly.
sorry for my ignorance but i’m not so sure what do you mean by needing additional %template lines?
do you mean the sort of things that’s happening in https://github.com/swig/swig/blob/master/Lib/std/std_sstream.i for example?

as i have not seen (sorry if i’ve missed it) in the documentation how i can specialise a templated function that takes templates as a function argument.

again sorry for the rather lame questions i’m just trying to understand how i could solve it and then maybe even as you mentioned contribute it back to swig.

again kudos to you all keep maintaining the project as the project i’m working on with is heavily depending on swig and your hard work to keep SWIG happening!

cheers,
viktor

> On 9 Jul 2017, at 9:44 PM, William S Fulton <[hidden email]> wrote:
>
>
>
> On 9 July 2017 at 11:37, Viktor Gal <[hidden email]> wrote:
> Hi
>
> based on the documentation of swig 3.0 "7.3.9 Type traits for metaprogramming” swig suppose to be able to handle type traits.
> i have the following templated function my c++ code:
>
> template <typename T>
> inline bool append_element(typename std::enable_if<!std::is_base_of<CSGObject, std::remove_pointer<T>>::value, T>::type e, const char* name=“”);
>
> and added the following %template directives into .i
>
> %template(append_element_real) append_element<double>;
> %template(append_element_float) append_element<float>;
>
> when i try to call from target language append_element_float, i’m getting
> NotImplementedError: Wrong number or type of arguments for overloaded function ‘append_element_float’
>
> Possible C/C++ prototypes are:
>     append_element< float32_t >(std::enable_if< !std::is_base_of<CSGObject,std::remove_pointer< float > >::value,float >::type,char const *)
>
> any ideas?
>
> or as i look into the code itself it’s just not possible to have this with swig support?
>
> You are missing a number of %template statements. Template are not automatically instantiated by SWIG, so you need a %template for each template being used, so in your case you need to also add one for each of std::enable_if, std::is_base_of, std::remove_pointer. SWIG does not provide a library interface file for the templates in the C++ header <type_traits>, like it does for those in <vector>, <list> etc. You can of course create your own interface file with the definitions of the templates that are necessary for SWIG to work. It would be nice if this were contributed back to the project for other SWIG users as we accept pull requests for enhancing the SWIG library with types in the C++ standard. They would need to go into a new std_type_traits.i file.
>
> William


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: type-traits handling and specialization

William S Fulton
Any use of a template requires instantiation if used from the target language. The documentation on templates is at http://swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_nn30. An 'empty template instantation' mentioned in that section is all you need for the traits types. As far as SWIG is concerned using template traits isn't much different to using templates using templates. Consider the case of one template being used in another template parameter:

%include <std_pair.i> // SWIG library file for std::pair
%include <std_vector.i> // SWIG library file for std::vector

%template(PairIntInt) std::pair<int, int>; // Requires instantiation before it can be used as a template argument elsewhere such as below
%template(VectorInt) std::vector<std::pair<int, int>>;

void foo(const std::vector<std::pair<int, int>>& x);

William

On 10 July 2017 at 03:13, Viktor Gal <[hidden email]> wrote:
Hi William,

thanks so much for getting back so quickly.
sorry for my ignorance but i’m not so sure what do you mean by needing additional %template lines?
do you mean the sort of things that’s happening in https://github.com/swig/swig/blob/master/Lib/std/std_sstream.i for example?

as i have not seen (sorry if i’ve missed it) in the documentation how i can specialise a templated function that takes templates as a function argument.

again sorry for the rather lame questions i’m just trying to understand how i could solve it and then maybe even as you mentioned contribute it back to swig.

again kudos to you all keep maintaining the project as the project i’m working on with is heavily depending on swig and your hard work to keep SWIG happening!

cheers,
viktor

> On 9 Jul 2017, at 9:44 PM, William S Fulton <[hidden email]> wrote:
>
>
>
> On 9 July 2017 at 11:37, Viktor Gal <[hidden email]> wrote:
> Hi
>
> based on the documentation of swig 3.0 "7.3.9 Type traits for metaprogramming” swig suppose to be able to handle type traits.
> i have the following templated function my c++ code:
>
> template <typename T>
> inline bool append_element(typename std::enable_if<!std::is_base_of<CSGObject, std::remove_pointer<T>>::value, T>::type e, const char* name=“”);
>
> and added the following %template directives into .i
>
> %template(append_element_real) append_element<double>;
> %template(append_element_float) append_element<float>;
>
> when i try to call from target language append_element_float, i’m getting
> NotImplementedError: Wrong number or type of arguments for overloaded function ‘append_element_float’
>
> Possible C/C++ prototypes are:
>     append_element< float32_t >(std::enable_if< !std::is_base_of<CSGObject,std::remove_pointer< float > >::value,float >::type,char const *)
>
> any ideas?
>
> or as i look into the code itself it’s just not possible to have this with swig support?
>
> You are missing a number of %template statements. Template are not automatically instantiated by SWIG, so you need a %template for each template being used, so in your case you need to also add one for each of std::enable_if, std::is_base_of, std::remove_pointer. SWIG does not provide a library interface file for the templates in the C++ header <type_traits>, like it does for those in <vector>, <list> etc. You can of course create your own interface file with the definitions of the templates that are necessary for SWIG to work. It would be nice if this were contributed back to the project for other SWIG users as we accept pull requests for enhancing the SWIG library with types in the C++ standard. They would need to go into a new std_type_traits.i file.
>
> William



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...