c – 模板类的模板化成员函数
发布时间:2020-12-16 07:03:13 所属栏目:百科 来源:网络整理
导读:我有一个模板化的C类,它也有一个模板化的成员函数.此成员函数的模板参数以特定方式依赖于类的模板参数(请参阅下面的代码). 我正在为这个类实例化(不专门化)它的模板参数的两个不同值.一切都汇编到这一点.但是,如果我调用模板化成员函数,则只调用第一个实例化
|
我有一个模板化的C类,它也有一个模板化的成员函数.此成员函数的模板参数以特定方式依赖于类的模板参数(请参阅下面的代码).
我正在为这个类实例化(不专门化)它的模板参数的两个不同值.一切都汇编到这一点.但是,如果我调用模板化成员函数,则只调用第一个实例化对象而不是第二个实例. 看起来好像编译器没有为模板类的第二个实例化实例化模板化成员函数.我正在使用“g filename.cpp”编译下面的代码,并收到以下错误: filename.cpp:63:错误:没有用于调用’Manager<(Base)1u> :: init(组合<(Base)1u,(Dependent2)0u> *)’的匹配函数 这是调用b.init(& combination_2)的行 g –version => g(Ubuntu / Linaro 4.4.7-1ubuntu2)4.4.7 uname -a => Linux 3.2.0-25-generic-pae#40-Ubuntu SMP i686 i686 i386 GNU / Linux enum Base {
AA,BB,CC
};
enum Dependent1 {
PP,QQ,RR
};
enum Dependent2 {
XX,YY,ZZ
};
template<Base B>
struct DependentProperty {
};
template<>
struct DependentProperty<AA> {
typedef Dependent1 Dependent;
};
template<>
struct DependentProperty<BB> {
typedef Dependent2 Dependent;
};
template <Base B,typename DependentProperty<B>::Dependent D>
class Combination {
public:
void reset() {}
int o;
};
template <Base B>
class Manager {
public:
template <typename DependentProperty<B>::Dependent D,template<Base,typename DependentProperty<B>::Dependent> class T>
void init(T<B,D>* t);
};
template <Base B>
template <typename DependentProperty<B>::Dependent D,typename DependentProperty<B>::Dependent> class T>
void Manager<B>::init(T<B,D>* t) {
t->reset();
}
int main(int argc,char** argv) {
Manager<AA> a;
Manager<BB> b;
Combination<AA,PP> combination_1;
Combination<BB,XX> combination_2;
a.init(&combination_1);
b.init(&combination_2);
return 0;
}
从我的实际项目中的示例代码修改与Base,Dependent或Combination对应的类是不可行的.我真正想知道的是我的定义Manager :: init()的语法是否错误,或者是否存在一些不允许此代码的已知属性/特征/约束的C或g? 解决方法
下面的代码为我编译,我已经简化了你的代码,虽然它仍然做同样的事情.
template <Base B>
class Manager {
public:
typedef typename DependentProperty<B>::Dependent D; // if ever you need it
template <typename TCombinaison>
void init(TCombinaison* t)
{
t->reset();
}
};
int main(int argc,char** argv)
{
typedef Combination<AA,PP> CombinaisonA;
typedef Combination<BB,XX> CombinaisonB;
typedef DependentProperty<AA> DependencyPropertyA;
typedef DependentProperty<BB> DependencyPropertyB;
CombinaisonA combination_1;
CombinaisonB combination_2;
Manager<AA> a;
Manager<BB> b;
a.init(&combination_1);
b.init<&combination_2);
return 0;
}
编辑:第二种解决方案,以禁止在管理人员中混合使用组合,正如OP在下面的评论中所注意到的那样.现在我使用std :: is_same来检查“概念”合同. template <Base B,typename DependentProperty<B>::Dependent D>
class Combination {
public:
typedef typename DependentProperty<B>::Dependent DependencyType;
void reset() {}
int o;
};
template <Base B>
class Manager {
public:
typedef typename DependentProperty<B>::Dependent DependencyType;
template <typename TCombinaison>
void init(TCombinaison* t)
{
static_assert(std::is_same<TCombinaison::DependencyType,Manager::DependencyType>);
t->reset();
}
};
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c – 在模板参数的方法中添加类型转换时出现clang错误
- Ruby是否具有与未定义的实例变量相当的method_missing?
- ruby-on-rails – 在Rails中,使用“has_many with belongs_
- react 实现pure render的时候,bind(this)隐患
- c# – 如何忽略SSL证书是由未知的证书颁发机构问题签名的?
- JSON一些基本概念笔记
- 【飞天奔月出品】Ajax提交数据之后,打开新窗口被浏览器拦截
- c# – 为什么GetProperties列出一个受保护的属性(在通用基类
- 免费的Flex拓扑图框架iolive
- oracle的归档模式 ORACLE数据库归档日志常用命令
