Sorry for the long title, here is what I'm trying to achieve: I have a small C++ class with a bool template parameter which, when true, disables its setter methods using std::enable_if
. Here is a simplified example:
template< bool IS_CONST >
class Handle
{
public:
Handle(void) : m_value(0) { }
Handle(int value) : m_value(value) { }
template < bool T = IS_CONST, typename COMPILED = typename std::enable_if< T == false >::type >
void set(int value)
{
m_value = value;
}
int get(void) const
{
return m_value;
}
private:
int m_value;
};
This code compiles an work as expected: Handle< true >
doesn't have the set
method, Handle< false >
has it.
Now I'm trying to bind this to Python using SWIG. I'm using the following file to generate the binding:
%module core
%include "Handle.h"
%template(NonConstHandle) Handle< false >;
%template(ConstHandle) Handle< false >;
%{
#include "Test.h"
%}
SWIG generates the module without complaining, it compiles fine, but the set
method is never bound, even in the specialized NonConstHandle
. e.g. the following Python test fails with AttributeError: 'NonConstHandle' object has no attribute 'set'
:
import core
handle = core.NonConstHandle()
assert(handle.get() == 0)
handle.set(1)
assert(handle.get() == 1)
const_handle = core.ConstHandle()
assert(const_handle .get() == 0)
try:
const_handle .set(1)
print("this should not print")
except:
pass
print("all good")
When I searched on the subject, I found lots of things related to enable_if and SWIG which leads me to think that it's supported, but I can't figure out why set
is not generated, although there's no error / warning emitted by SWIG...
Any help appreciated ! Regards