在Swift中使用C 模板
Use C++ Template in Swift
这是我的CPP模板:
template <typename T>
class CppTemplate1
{
public:
CppTemplate1(){}
T Mul(T a, T b){ return a*b; }
};
我在OBJ C中实例化:
@interface templatetest<__covariant T: id>()
@property CppTemplate1<T> m_temp;
@end
@implementation templatetest
- (instancetype)init {
return self;
}
- (_Nullable id)Mult: (_Nullable id) a sec: (_Nullable id) b{
return _m_temp.Mul(a, b);
}
@end
现在我想在Swift中使用它:
let m = templatetest<Int>()
let s = m?.mult(3, sec: 5)
print(s)
我在第一个Swift行中有此编译错误:
'Int' is not convertible to 'AnyObject'
我认为您在Objective-C 中实例化,而不是Objective-C。
由于模板在编译时进行了扩展,因此无法直接从不基于C 的任何语言中消耗它们。
相关文章:
- .cpp和.h文件中的模板专用化声明
- C++模板来检查友元函数的存在
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如何在c++中为模板函数实例创建快捷方式
- 使用C++中的模板和运算符重载执行矩阵运算
- 有人能分解一下这个c++模板的语法吗
- 如何在c++17中制作一个模板包装器/装饰器
- 模板化建造师专业化
- 调用专用模板时出错"no matching function for call to [...]"
- 模板元程序查找相似的连续类型名称
- 如何在C++20中创建模板别名的推导指南
- 没有名称的C++模板参数
- 具有重复类型的C++可变模板
- 如何将enable-if与模板参数和参数包一起使用
- 在Swift中使用C 模板