簡體   English   中英

C ++模板函數 - >將模板類作為模板參數傳遞

[英]C++ template-function -> passing a template-class as template-argument

我嘗試大量使用模板來包裝工廠類:

包裝類(即classA)通過template-argument獲取包裝類(即classB)以提供“可插拔性”。

另外,我必須提供一個繼承自包裝的內部類(innerB)的內部類(innerA)。

問題是g ++“gcc版本4.4.3(Ubuntu 4.4.3-4ubuntu5)”的以下錯誤消息:

sebastian@tecuhtli:~/Development/cppExercises/functionTemplate$ g++ -o test test.cpp
test.cpp: In static member function ‘static classA<A>::innerA<iB>* classA<A>::createInnerAs(iB&) [with iB = int, A = classB]’:
test.cpp:39:   instantiated from here
test.cpp:32: error: dependent-name ‘classA::innerA<>’ is parsed as a non-type, but instantiation yields a type
test.cpp:32: note: say ‘typename classA::innerA<>’ if a type is meant

正如您在方法createInnerBs的定義中所看到的,我打算傳遞一個非類型參數。 所以使用typename是錯誤的!

test.cpp的代碼如下:

class classB{
public:
  template < class iB>
  class innerB{
    iB& ib;
    innerB(iB& b)
      :ib(b){}
  };

  template<template <class> class classShell, class iB>
  static classShell<iB>* createInnerBs(iB& b){
    // this function creates instances of innerB and its subclasses, 
    // because B holds a certain allocator

    return new classShell<iB>(b);
  }  
};

template<class A>
class classA{
  // intention of this class is meant to be a pluggable interface
  // using templates for compile-time checking
public:
  template <class iB>
  class innerA: A::template innerB<iB>{
    innerA(iB& b)
      :A::template innerB<iB>(b){}
  };

  template<class iB>
  static inline innerA<iB>* createInnerAs(iB& b){
    return A::createInnerBs<classA<A>::template innerA<> >(b); // line 32: error occurs here
  }
};

typedef classA<classB> usable;
int main (int argc, char* argv[]){
  int a = 5;
  usable::innerA<int>* myVar = usable::createInnerAs(a);

  return 0;
}

請幫助我,我已經面臨這個問題好幾天了。 這是不可能的,我想做什么? 還是我忘記了什么?

謝謝,Sema

第32行應為:

return A::template createInnerBs<innerA>(b);

因為createInnerBs依賴於模板參數A

您還需要將innerAinnerB的構造函數innerB public。

這是為我編譯的更正代碼:

class classB{ 
public: 
  template < class iB> 
  class innerB{ 
    iB& ib; 
  public:
    innerB(iB& b) 
      :ib(b){} 
  }; 

  template<template <class> class classShell, class iB> 
  static classShell<iB>* createInnerBs(iB& b){ 
    // this function creates instances of innerB and its subclasses,  
    // because B holds a certain allocator 

    return new classShell<iB>(b); 
  }   
}; 

template<class A> 
class classA{ 
  // intention of this class is meant to be a pluggable interface 
  // using templates for compile-time checking 
public: 
  template <class iB> 
  class innerA: public A::template innerB<iB>{ 
  public:
    innerA(iB& b) 
      : A::template innerB<iB>(b){} 
  }; 

  template<class iB> 
  static inline innerA<iB>* createInnerAs(iB& b);
}; 

template<class A> 
template<class iB> 
inline classA<A>::innerA<iB>* classA<A>::createInnerAs(iB& b)
{ 
    return A::template createInnerBs<classA::template innerA>(b);
} 

typedef classA<classB> usable; 
int main (int argc, char* argv[]){ 
  int a = 5; 
  usable::innerA<int>* myVar = usable::createInnerAs(a); 

  return 0; 
} 

即使我認為你的事情過於復雜......但我並不完全理解你的用例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM