以下代码是否合法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…如果未以其他方式实例化虚拟成员函数,则不确定实现是否隐式实例化类模板的虚拟成员函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句