如何解决不合格的名称查找问题
how to solve unqualified name lookup problem
我有以下简化程序:
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 ;
}
现在的问题是,有没有办法简化完整的重复
基本 class 类型。我的原始代码是这样的:
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=;
};
如您所见,所有模板参数的完全重复并不是很"nice"。有机会解决这个问题吗?
如果上面的代码没有依赖模板参数(将单行 1.) 从上面的示例更改为:
CONTAINERSP< DataStoreBase, int>
class 变得非常简单且更易于维护:
...
VarObserved( const VAR_TYPE& var ): ConstructAll2{{ var }}{}
using AssignConst::operator=;
....
作为对潜在问题的参考,我已经找到了那个问题
"not declared in this scope" error with templates and inheritance
但不知道如何简化我的代码。
也许使用具有默认值的新模板参数(根据 Justin 的观察更正)?
template <typename VAR_TYPE, typename CA2 = ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE >>>
class VarObserved : public CA2
{
public:
using SELF = CA2;
VarObserved( const VAR_TYPE& var ): SELF{{ var }}{}
using SELF::AssignConst::operator=;
};
除了 max66 的想法之外,我还找到了以下解决方案,不是基于专业化而是基于模板模板参数:
template < typename VAR_TYPE >
using VarObserved_Parms = ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE >
>;
template < typename VAR_TYPE, template <typename> class COMPONENTS >
class VarObserved2: public COMPONENTS< VAR_TYPE >
{
using SELF = COMPONENTS< VAR_TYPE >;
VarObserved2( const VAR_TYPE& var ): SELF{{ var }}{}
using SELF::AssignConst::operator=;
};
在我从 Brian 那里得到想法后,一切都崩溃了:
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:
VarObserved( const VAR_TYPE& var ): VarObserved::ConstructAll2{{ var }}{}
^^^^^^^^^^^^^ never saw that before, interesting syntax ;)
using VarObserved::AssignConst::operator=;
};
The question is now, if there is any way to simplify the full repetition of the base class type.
要查找在依赖基 class 中声明的名称 TOP
,您可以写 Y::TOP
而不是 X<T>::TOP
,尽管这可能会使读者感到困惑,您应该可能会对你为什么这样做发表评论。
之所以可行,是因为它不再是不合格的查找。请注意,您不需要在此处写出模板参数(Y
是注入的 class 名称,与 Y<T>
的含义相同)。
我有以下简化程序:
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 ;
}
现在的问题是,有没有办法简化完整的重复 基本 class 类型。我的原始代码是这样的:
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=;
};
如您所见,所有模板参数的完全重复并不是很"nice"。有机会解决这个问题吗?
如果上面的代码没有依赖模板参数(将单行 1.) 从上面的示例更改为:
CONTAINERSP< DataStoreBase, int>
class 变得非常简单且更易于维护:
...
VarObserved( const VAR_TYPE& var ): ConstructAll2{{ var }}{}
using AssignConst::operator=;
....
作为对潜在问题的参考,我已经找到了那个问题
"not declared in this scope" error with templates and inheritance
但不知道如何简化我的代码。
也许使用具有默认值的新模板参数(根据 Justin 的观察更正)?
template <typename VAR_TYPE, typename CA2 = ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE >>>
class VarObserved : public CA2
{
public:
using SELF = CA2;
VarObserved( const VAR_TYPE& var ): SELF{{ var }}{}
using SELF::AssignConst::operator=;
};
除了 max66 的想法之外,我还找到了以下解决方案,不是基于专业化而是基于模板模板参数:
template < typename VAR_TYPE >
using VarObserved_Parms = ConstructAll2<
UsingHelperV_None,
UsingHelper_None,
CONTAINERL< AssignConst >,
CONTAINERL< Print >,
CONTAINERL< DataStore >,
CONTAINERL< Distribute_ >,
CONTAINERL< EndForward >,
CONTAINERSP< DataStoreBase, VAR_TYPE >
>;
template < typename VAR_TYPE, template <typename> class COMPONENTS >
class VarObserved2: public COMPONENTS< VAR_TYPE >
{
using SELF = COMPONENTS< VAR_TYPE >;
VarObserved2( const VAR_TYPE& var ): SELF{{ var }}{}
using SELF::AssignConst::operator=;
};
在我从 Brian 那里得到想法后,一切都崩溃了:
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:
VarObserved( const VAR_TYPE& var ): VarObserved::ConstructAll2{{ var }}{}
^^^^^^^^^^^^^ never saw that before, interesting syntax ;)
using VarObserved::AssignConst::operator=;
};
The question is now, if there is any way to simplify the full repetition of the base class type.
要查找在依赖基 class 中声明的名称 TOP
,您可以写 Y::TOP
而不是 X<T>::TOP
,尽管这可能会使读者感到困惑,您应该可能会对你为什么这样做发表评论。
之所以可行,是因为它不再是不合格的查找。请注意,您不需要在此处写出模板参数(Y
是注入的 class 名称,与 Y<T>
的含义相同)。