覆盖使用模板参数包扩展声明的虚拟方法时遇到问题
Having trouble overriding a virtual method declared with template parameter pack expansion
我在重写使用模板参数包扩展指定的基类的虚拟方法时遇到问题 - 而覆盖方法将显示实际的相关类型。这是一个MCVE:
#include <iostream>
template <typename... Ts>
class A { virtual void foo(Ts&&...); };
class B : public A<int, unsigned> {
void foo(int x, unsigned y) override { std::cout << "here"; }
};
int main() {
B b;
}
编译这个(标准设置为 C++11 或 C++14),我得到:
a.cpp:9:7: error: ‘void B::foo(int, unsigned int)’ marked override, but does not override void foo(int x, unsigned y) override { ^
基类的函数签名是 void foo(Ts&&...);
。
派生类的函数签名为 void foo(int x, unsigned y)
。
看到两者之间有什么不同吗?区别是&&
.为了匹配基类的函数签名,您需要派生类使用 void foo(int&& x, unsigned&& y)
。
演示:
#include <iostream>
template <typename... Ts>
struct A { virtual void foo(Ts&&...) {} };
struct B : A<int, unsigned> {
void foo(int&& x, unsigned&& y) override { std::cout << "here"; }
};
int main() {
B b;
b.foo(1, 2u);
}
相关文章:
- 基类可以声明虚拟方法但不定义它吗?仍然在派生类中定义
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 有什么理由在没有继承的情况下声明一个虚拟方法
- C++如何使用虚拟基类型声明全局静态分配的变量
- 我们如何有证据表明,声明虚拟函数的类是2个字节,其中一个不超过一个未声明,而在子类中
- C 是纯虚拟函数,也是一个定义或仅声明
- 当声明了虚拟析构函数但没有实现时会发生什么情况
- 为什么我需要在子类中重新声明"虚拟"方法?[C++/多态性]
- 如何使用派生类中的虚拟函数,该函数在另一个具有基类参数的类中声明
- 不能声明Qwidget,因为虚拟函数是纯粹的
- 用派生类对象作为参数在基类中声明纯虚拟函数
- 在模板基类的虚拟函数中使用前向声明类,其中构造函数只需要前向声明
- 在基没有虚拟方法的派生类中声明虚拟方法是错误的吗
- 两次声明纯虚拟函数(在层次结构中的两个类中)是否完全合法
- 应该在所有继承级别还是仅在祖先级别声明虚拟函数
- 覆盖使用模板参数包扩展声明的虚拟方法时遇到问题
- 如何在不中断移动和复制构造函数的情况下声明虚拟析构函数
- 缺少vtable:是否应该在头文件中重新声明虚拟函数
- 在声明虚拟方法的父分支之外的其他父分支中实现虚拟方法
- 函数声明(虚拟无效打印标记表() const = 0;)中函数名称后"Type Qualifier (const)"是什么意思?