C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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;
}

从我的实际项目中的示例代码修改@R_431_8801@,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 – 模板类的模板化成员函数全部内容,希望文章能够帮你解决c – 模板类的模板化成员函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。