使用语法在派生 class 中公开基 class 别名模板和变量模板?
Using syntax to expose base class alias templates and variable templates in derived class?
在繁重的模板元编程上下文中考虑基本模板 class 和派生模板 class(为了便于阅读和关注问题,此处进行了简化)。
template <class T>
struct base {
using type = T;
static constexpr int value = 0;
template <class... Args>
constexpr void function(Args&&...) {}
template <class U>
using alias_template = base<U>;
template <class U>
static constexpr int variable_template = 0;
};
template <class T>
struct derived: base<T> {
using typename base<T>::type; // Using base typedef
using base<T>::value; // Using base static data member
using base<T>::function; // Using base function members (regardless of template or not)
//using typename base<T>::alias_template; // DOES NOT SEEM TO WORK
//using base<T>::variable_template; // DOES NOT SEEM TO WORK
using typedef_test = type; // Working
static constexpr int value_test = value; // Working
using alias_template_test = alias_template<T>; // ERROR
static constexpr int variable_template_test = variable_template<T>; // ERROR
};
问题:是否有 using
语法来公开从基础 class 继承的别名模板和变量模板,以便使当前错误的行编译?是否有任何解决方法可以避免在派生的 class 中每次都指定 base<T>::
(这里它仍然很简单,但在我的实际代码中,每次快速指定变得很烦人)?
using
doesn't work this way 对于依赖成员模板:
A using-declaration also can't be used to introduce the name of a dependent member template as a template-name (the template disambiguator for dependent names is not permitted)
我不确定这是否会满足您的要求,但为了使别名像模板一样工作,您可以在派生的 [=] 中声明 new 成员模板24=] 根据基的成员模板定义:
template<typename U>
using alias_template = typename base<T>::template alias_template<U>;
template<typename U>
static constexpr auto variable_template = base<T>::template variable_template<U>;
然而,IMO 每次都指定 base<T>::
不是问题,并且比引入新模板更干净。您甚至可以使用 using Base = my_long_base_class_name<T>;
之类的内容使其更短
在繁重的模板元编程上下文中考虑基本模板 class 和派生模板 class(为了便于阅读和关注问题,此处进行了简化)。
template <class T>
struct base {
using type = T;
static constexpr int value = 0;
template <class... Args>
constexpr void function(Args&&...) {}
template <class U>
using alias_template = base<U>;
template <class U>
static constexpr int variable_template = 0;
};
template <class T>
struct derived: base<T> {
using typename base<T>::type; // Using base typedef
using base<T>::value; // Using base static data member
using base<T>::function; // Using base function members (regardless of template or not)
//using typename base<T>::alias_template; // DOES NOT SEEM TO WORK
//using base<T>::variable_template; // DOES NOT SEEM TO WORK
using typedef_test = type; // Working
static constexpr int value_test = value; // Working
using alias_template_test = alias_template<T>; // ERROR
static constexpr int variable_template_test = variable_template<T>; // ERROR
};
问题:是否有 using
语法来公开从基础 class 继承的别名模板和变量模板,以便使当前错误的行编译?是否有任何解决方法可以避免在派生的 class 中每次都指定 base<T>::
(这里它仍然很简单,但在我的实际代码中,每次快速指定变得很烦人)?
using
doesn't work this way 对于依赖成员模板:
A using-declaration also can't be used to introduce the name of a dependent member template as a template-name (the template disambiguator for dependent names is not permitted)
我不确定这是否会满足您的要求,但为了使别名像模板一样工作,您可以在派生的 [=] 中声明 new 成员模板24=] 根据基的成员模板定义:
template<typename U>
using alias_template = typename base<T>::template alias_template<U>;
template<typename U>
static constexpr auto variable_template = base<T>::template variable_template<U>;
然而,IMO 每次都指定 base<T>::
不是问题,并且比引入新模板更干净。您甚至可以使用 using Base = my_long_base_class_name<T>;