c++ - 将模板化的基类转换运算符引入派生范围

标签 c++ templates

我有一个基类,定义了受约束的模板化转换运算符

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/

相关文章:

c++ - 使用 ide qt 在 C++ 中将 'const' 作为 'this' 参数传递时出错

c++ - 如何在 C++ 中获取文件时间

来自 USB 的 C++ 和 Lua

c++ - (signed) long long 的真正下限是多少?

c++ - 如何使用 for 循环回到起点?

C++ 返回泛型类型集合的正确方法

c++ - 可变递归模板

c# - kendo 网格控件中的 DropDownList(通过 ClientTemplate)

c++ - 有没有办法在右值和左值引用中创建函数时避免重复代码?

c++ - Lambda 作为模板函数