我有以下简化程序:
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
但不知道如何简化我的代码。
最佳答案
The question is now, if there is any way to simplify the full repetition of the base class type.
查找名称TOP
在依赖基类中声明,可以写Y::TOP
而不是X<T>::TOP
,尽管这可能会让读者感到困惑,但您可能应该评论一下为什么要这样做。
之所以有效,是因为它不再是不合格的查找。请注意,您不需要在此处编写模板参数( Y
是注入(inject)的类名,与 Y<T>
含义相同)。
关于c++ - 如何解决不合格名称查找问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57044138/