Discussion:
[Swig-user] type-traits handling and specialization
Viktor Gal
2017-07-09 10:37:43 UTC
Permalink
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
William S Fulton
2017-07-09 13:44:22 UTC
Permalink
Post by 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.
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’
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
Viktor Gal
2017-07-10 02:13:43 UTC
Permalink
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
Post by 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.
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’
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
William S Fulton
2017-07-10 07:00:36 UTC
Permalink
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
Post by 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
Post by 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.
Post by Viktor Gal
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=“”);
Post by Viktor Gal
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’
Post by Viktor Gal
append_element< float32_t >(std::enable_if<
!std::is_base_of<CSGObject,std::remove_pointer< float > >::value,float
Post by Viktor Gal
::type,char const *)
any ideas?
or as i look into the code itself it’s just not possible to have this
with swig support?
Post by Viktor Gal
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.
Post by Viktor Gal
William
Loading...