开发者

Getting a type for a template instantiation?

I have the following situation:

I have a object of type MyClass, which has a method to cast itself to its base class. The class includes a typedef for its base class and a method to do the upcast.

template <class T, class B>
class BaseClass;

template <class T>
class NoAccess;

template <class T>
class MyClass : public BaseClass<T, NoAccess<T> >
{
  private:
    typedef BaseClass<T, NoAccess<T> > base;

  public:
    base &to_base();
};

I need to pass the result of a base call to a functor Operator:

template <class Y>
class Operator
{
   Operator(Y &x);
};

Operator<???> op(myobject.to_base());开发者_StackOverflow

Is there a easy way to fill the ??? provided that I do not want to use NoAccess?


You can define a factory function to use type deduction:

template< class Y >
Operator<Y> create_operator( Y &y ) {
    return Operator<Y>( y );
}


I see three possibilities:

  1. make MyClass<T,B>::base a public identifier

  2. change your operator and make the constructor a template using type-erasure (might be hard):
    class Operator { template<class Y> Operator(Y &x); }

  3. move the code creating the Operator object into its own function template:
    template<typename T> Operator<T> createOperator(const T& base)
    {return Operator<T>(base);}


You can make the typedef public:

Operator<MyClass<type>::base> op(myobject.to_base());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜