簡體   English   中英

MSVC 與 Clang/GCC 錯誤在函數模板的重載解決期間,其中一個包含參數包

[英]MSVC vs Clang/GCC bug during overload resolution of function templates one of which contains a parameter pack

當我注意到一個這樣的案例(如下所示)在 gcc 和 clang 中編譯良好但在 msvc 中編譯時,我正在使用參數包:

template<class T> void func(T a, T b= T{})
{
    
}
template<class T, class... S> void func(T a, S... b)
{

}
 
int main()
{
    func(1); // Should this call succeed? 
}

這是驗證相同的鏈接: https ://godbolt.org/z/8KsrcnMez

可以看出,上面的程序在 msvc 中失敗並顯示錯誤消息:

<source>(13): error C2668: 'func': ambiguous call to overloaded function
<source>(6): note: could be 'void func<int,>(T)'
        with
        [
            T=int
        ]
<source>(2): note: or       'void func<int>(T,T)'
        with
        [
            T=int
        ]
<source>(13): note: while trying to match the argument list '(int)'

但是使用 gcc 和 clang 也可以很好地編譯。

哪個編譯器在這里?

MSVC 拒絕代碼是正確的。 根據temp.func.order#5.example-2調用func(1)是模棱兩可的。 標准中給出的例子如下:

注意:由於在調用上下文中,這種類型推導只考慮具有顯式調用參數的參數,因此忽略某些參數(即函數參數包、具有默認參數的參數和省略號參數)

 template<class T > void g(T, T = T()); // #3 template<class T, class... U> void g(T, U ...); // #4 void h() { g(42); // error: ambiguous }

這意味着在所討論的示例中,調用func(1)也是模棱兩可的。

勘誤表

請注意, cppreference在下面給出的示例中有勘誤表:

 template<class T> void g(T, T = T()); // #1 template<class T, class... U> void g(T, U...); // #2 void h() { g(42); // calls #1 due to the tie-breaker between parameter pack and omitted parameter }

從上面的例子可以看出,cpprefernce 錯誤地表示應該選擇第一個重載#1

這已在我最近進行的編輯中得到修復。

暫無
暫無

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

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