我有一个基类,定义了受约束的模板化转换运算符
struct base {
template <typename C, std::enable_if_t<some_constraints<C>, int> = 0>
operator C() const;
};
我也有一个派生类,该类实现了具有不同约束的另一个转换运算符:
struct derived : base {
template <typename P, std::enable_if_t<different_constraints<P>, int> = 0>
operator P() const;
};
不幸的是,派生类中的声明将运算符隐藏在基类中。我想将基本运算符引入派生范围,但使用“显而易见的”语法
template <typename C>
using base::operator C;
不起作用(编译器似乎尝试将其解析为别名模板声明)。
有谁知道实现此目标的正确语法?
最佳答案
我会说这是不可能的。即使是这样,您的派生运算符也会将基数1隐藏,因为根据namespace.udecl#15.sentence-1,模板参数不属于该参数:
When a using-declarator brings declarations from a base class into a derived class, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list, cv-qualification, and ref-qualifier (if any) in a base class (rather than conflicting)
不幸的是,模板参数不计算在内,两个转换
operator
都有空的parameter-type-list,是const
,没有ref限定符。
关于c++ - 将模板化的基类转换运算符引入派生范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63349221/