簡體   English   中英

為什么此函數指針的可變參數模板參數推導失敗?

[英]Why does the variadic template argument deduction fail for this function pointer?

在下面的最小示例中, S::foo可以工作,但S::bar失敗。

唯一的區別是參數包TsUs的順序。

struct FPtrS::lol是我發現的最好的解決方法,但在實踐中使用起來相當不舒服。

為什么bar的參數推論失敗(特別是因為我已經明確指定了類型,所以根本不應該進行演繹)? 這是一個編譯器錯誤(與clang++ 3.5g++ 4.9 ),還是出於某種原因,它是否在標准中?

template<typename ... Ts>
struct FPtr {
    FPtr(void (*val)(Ts ...)) : val{val} {}

    void (*val)(Ts ...);
};


template<typename ... Ts>
struct S {
    template<typename ... Us>
    void lol(FPtr<Us ..., Ts ...>) {}

    template<typename ... Us>
    void foo(void (*)(Ts ..., Us ...)) {}

    template<typename ... Us>
    void bar(void (*)(Us ..., Ts ...)) {}
};


void f(int, float) {}
void g(float, int) {}


int main() {
    S<int> s;

    s.lol<float>(FPtr<float, int>(g));
    s.foo<float>(f);
    s.bar<float>(g);
}

錯誤消息是:

$ clang++ -std=c++14 t27.cpp -Wall -Wextra -pedantic
t27.cpp:31:4: error: no matching member function for call to 'bar'
        s.bar<float>(g);
        ~~^~~~~~~~~~
t27.cpp:18:7: note: candidate template ignored: failed template argument deduction
        void bar(void (*)(Us ..., Ts ...)) {}
             ^

注意:我在GCCLLVM錯誤跟蹤器上報告了此錯誤。

我已經用Clang和GCC測試了這段代碼,他們都無法編譯程序。 我會說這是兩個編譯器中的錯誤。 在參數列表結束之前出現的函數參數包是非推導的上下文。 在替換顯式指定的模板參數后,它應該構建函數

template<>
S<int>::bar(void (*)(float, int));

哪個應該匹配電話。 Clang和GCC以前在這樣的領域遇到過問題,而且他們的診斷技術已經不太有用了。 然而令人驚訝的是VC ++編譯代碼。

考慮以下哪些在兩個編譯器下都有效。

template<class... Ts>
struct S {
    template<class... Us>
    void bar(Us..., Ts...);
};

int main() {
    S<int>().bar<int>(1, 2);
}

您的程序具有相同的語義,應該平等對待。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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