簡體   English   中英

模板類方法實例化當基類中的虛擬無關方法導致MSVC上的編譯失敗時

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM