I got an error while trying to generate a SWIG interface for a library I wanted to use. The code contains a class which inherits from a templated class, that include default values. However, the templated class also has a forward declaration that doesn't include defaults. I believe this is confusing swig.
Here's a simple example:
frac.h
(parent class):
#pragma once
// forward declaration
template <typename A, typename B>
class Frac;
// ... code using the forward declaraton
// definition
template <typename A=int, typename B=int>
class Frac
{
public:
A a;
B b;
double divide()
{
return a / b;
};
};
timestwo.h
(child class):
#pragma once
#include "frac.h"
class TimesTwo : public Frac<double>
{
public:
double getValue()
{
a = 10.5;
b = 4;
return divide() * 2;
}
};
mylib.i
file:
%module mylib
%{
#include "timestwo.h"
%}
%include "frac.h"
/*
If no %template is used:
mylib.h:15: Warning 401: Nothing known about base class 'Frac< double >'. Ignored.
mylib.h:15: Warning 401: Maybe you forgot to instantiate 'Frac< double >' using %template.
*/
/*
If put here: %template(frac_d) Frac <double>;
mylib.i:15: Error: Not enough template parameters specified. 2 required.
*/
/*
If put here: %template(frac_d) Frac <double, int>;
timestwo.h:5: Warning 401: Nothing known about base class 'Frac< double >'. Ignored.
timestwo.h:5: Warning 401: Maybe you forgot to instantiate 'Frac< double >' using %template.
*/
%include "timestwo.h"
As shown in the comments of mylib.i
, I can't seem to instantiate the template correctly, since I need to use one template argument, but since the forward declaration doesn't specify the defaults, it says it's expecting two.