[英]template class method instantiation when a virtual unrelated method in the base class causes compilation failure on MSVC
以下代码是否合法C ++?
MS Visual C ++失败了,但gcc和clang很好: https : //godbolt.org/z/vsQOaW
它可能是一个msvc错误,但想先检查一下:
struct Base {
virtual void junk() = 0;
};
template <class T>
struct Derived : Base {
void junk() override {
T::junkImpl();
}
void otherMethod() {
}
};
template <class T>
struct NotDerived {
void junk() {
T::junkImpl();
}
void otherMethod() {
}
};
struct TypeWithJunk {
void junkImpl() {
}
};
struct TypeWithoutJunk {};
void reproduce(NotDerived<TypeWithoutJunk>* ndt, Derived<TypeWithoutJunk>* dt) {
// works - junk is not used, not instantiated
ndt->otherMethod();
// fails on MSVC - junk is instantiated even if not used
dt->otherMethod();
}
junk
可能像其他虚函数一样被实例化,因为它需要填充vtable。 因此,所有编译器似乎都表现出一致的行为:
17.8.1隐式实例化[temp.inst]
9 ...如果虚拟成员函数不会被实例化,则实现是否隐式实例化类模板的虚拟成员函数是未指定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.