【发布时间】:2019-07-15 16:53:32
【问题描述】:
我有以下简化程序:
class Base { };
template < typename T >
class X: public T
{
public:
using TOP = T;
};
// with dependent template parm
template < typename T >
class Y: public X< T >
{
// I have to write down the full name of the base class
using X<T>::TOP::operator =;
};
// without depending template parameter
template < typename T >
class Y: public X< Base >
{
// I simply can use the defined type from the base class
using TOP::operator =;
};
int main()
{
Y< Base > y ;
}
现在的问题是,是否有任何方法可以简化完整的重复 基类类型。我的原始代码是这样的:
template < typename VAR_TYPE >
class VarObserved: public ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE >
>
{
public:
using SELF = ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE > // see text 1)
>;
VarObserved( const VAR_TYPE& var ): SELF{{ var }}{}
using SELF::AssignConst::operator=;
};
如您所见,所有模板参数的完全重复并不是很“好”。有机会解决吗?
如果上面的代码没有依赖模板参数(将单行1.))从上面的示例中改为:
CONTAINERSP< DataStoreBase, int>
这个类变得非常简单并且更容易维护:
...
VarObserved( const VAR_TYPE& var ): ConstructAll2{{ var }}{}
using AssignConst::operator=;
....
作为对潜在问题的参考,我已经发现了那个问题
"not declared in this scope" error with templates and inheritance
但不知道如何简化我的代码。
【问题讨论】:
-
更正了我的答案(贾斯汀观察到,不需要专业化);对不起。
标签: c++ templates unqualified-name