从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
SWIG error when inheriting from partially-specified template class with defaults, with forward declaration without defaults
我在尝试为要使用的库生成SWIG接口时出错。该代码包含一个从模板化类继承的类,该类包含默认值。但是,模板化类也有一个不包含默认值的正向声明。我相信这是令人困惑的swig。
这里有一个简单的例子:
frac.h
(父类(:
#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
(子类(:
#pragma once
#include "frac.h"
class TimesTwo : public Frac<double>
{
public:
double getValue()
{
a = 10.5;
b = 4;
return divide() * 2;
}
};
mylib.i
文件:
%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"
如mylib.i
的注释所示,我似乎无法正确实例化模板,因为我需要使用一个模板参数,但由于forward声明没有指定默认值,它表示需要两个。
这只是一个警告。您想实例化Frac
还是调用divide
?否则,它会起作用:
>>> import mylib
>>> t = mylib.TimesTwo()
>>> t.getValue()
5.25
如果您想调用divide()
,SWIG似乎不了解模板默认值。它通过用Frac<double,int>
更新timestwo.h
来工作,但如果你不想修改头,你可以手动复制.i
文件中的定义,并更正:
%module mylib
%{
#include "timestwo.h"
%}
%include "frac.h"
%template(frac_d) Frac<double,int>; // Frac<double> doesn't work as of SWIG 3.0.12.
// Declare the interface the way SWIG likes it.
class TimesTwo : public Frac<double,int>
{
public:
double getValue();
};
演示:
>>> import mylib
>>> t = mylib.TimesTwo()
>>> t.getValue()
5.25
>>> t.divide()
2.625
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 具有默认值的引用获取函数
- 当给定默认值时,为什么此模板参数推导失败
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 格式化浮点值:返回默认值
- 如何将数组部分初始化为某个默认值?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 创建一个包含 c++ 默认值的环境文件
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- Makefile g++ 使用命令行中的 -D 变量进行编译,默认值
- C++中出现意外的编译错误:将默认值传递给函数参数
- <可变参数模板> Visual Studio 中具有默认值编译错误的模板参数
- 将错误处理添加到加载数据的函数(但有时还需要使用默认值)
- 将指针指向函数的函数和将默认值作为参数的向量会导致编译错误
- 默认参数值错误 [Visual C++ 2008 中的错误?
- 为什么 boost::p rogram_options 在定义默认值时会抛出有关必需参数的错误
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 指针对成员函数默认值出现与CRTP相关的编译器错误
- C'Tor 的 C++ 返回值 + 错误处理的默认返回值
- 输入错误/无效的输入时,如何设置默认值