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:
make
MyClass<T,B>::base
apublic
identifierchange your operator and make the constructor a template using type-erasure (might be hard):
class Operator { template<class Y> Operator(Y &x); }
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());
精彩评论