C ++模板函数 - >将模板类作为模板参数传递

时间:2010-06-02 09:32:47

标签: c++

我尝试大量使用模板来包装工厂类:

包装类(即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

2 个答案:

答案 0 :(得分:2)

第32行应为:

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

因为createInnerBs取决于模板参数A

您还需要将innerAinnerB的构造函数设为公开。

答案 1 :(得分:1)

以下是为我编译的更正代码:

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; 
} 

即使我认为你的事情过于复杂......但我并不完全理解你的用例。