开发者

C++ template-function -> passing a template-class as template-argument

i try to make intensive use of templates to wrap a factory class:

The wrapping class (i.e. classA) gets the wrapped class (i.e. classB) via an template-argument to provide 'pluggability'.

Additionally i have to provide an inner-class (innerA) that inherits from the wrapped inner-class (innerB).

The problem is the following error-message of the g++ "gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)":

sebastian@tecuhtli:~/Development/cppExe开发者_开发问答rcises/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

As you can see in the definition of method createInnerBs, i intend to pass a non-type argument. So the use of typename is wrong!

The code of test.cpp is below:

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

Please help me, i have been faced to this problem for several days. Is it just impossible, what i'm trying to do? Or did i forgot something?

Thanks, Sema


Line 32 should read:

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

since createInnerBs is dependent on the template parameter A.

You'll also need to make the constructors of innerA and innerB public.


Here is the corrected code which compiles for me:

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

Even if I think you're overcomplicating things... But I don't fully understand your use case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜