2013-02-02 29 views
9

我有這樣的代碼(從一個更復雜的簡化版本):可變參數模板的代碼編譯的GCC 4.6,但不是在鐺或GCC 4.7

template <class... A1> class Test { 
    public: 
    template <class... A2> void print (void(*function)(A2...,A1...)) { 

    } 
}; 

void test_print (int a, float b, double c) { 

} 

int main() { 
    Test<float,double> test; 
    test.print<int> (&test_print); 
} 

如果我使用g++ -std=c++0x filename.cpp它編譯編譯它GCC 4.6.3然而細使用clang++ -std=c++0x filename.cpp它引發以下錯誤鐺3.0:

filename.cpp:14:10: error: no matching member function for call to 'print' 
    test.print<int> (&test_print); 
    ~~~~~^~~~~~~~~~ 
filename.cpp:3:33: note: candidate template ignored: failed template argument deduction 
    template <class... A2> void print (void(*function)(A2...,A1...)) { 
           ^
1 error generated. 

在GCC 4.7.2也有一個錯誤:

filename.cpp: In function 'int main()': 
filename.cpp:14:33: error: no matching function for call to 'Test<float, double>::print(void (*)(int, float, double))' 
filename.cpp:14:33: note: candidate is: 
filename.cpp:3:33: note: template<class ... A2> void Test::print(void (*)(A2 ..., A1 ...)) [with A2 = {A2 ...}; A1 = {float, double}] 
filename.cpp:3:33: note: template argument deduction/substitution failed: 
filename.cpp:14:33: note: mismatched types 'float' and 'int' 

現在的問題是:它爲什麼失敗或我做錯了什麼?

+0

您是否嘗試刪除''? C++在可能的情況下使用模板類型演繹。 – leemes

+0

g ++ 4.7錯誤信息:http://ideone.com/qwSKJl參數排序似乎有問題。 – leemes

+0

@眼鏡:我試過了,它沒有幫助 – eyelash

回答

2

作爲解析http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1399的一部分,確定不可推導的參數包不參與推導,因此保留參數列表中明確指定的參數,但不干擾後續參數與後續函數的匹配參數。然而,這些添加的規則不一定適用於比較類型時的扣減(這是扣除參數集與函數調用參數的子算法)。

我認爲決議應該適用於您的情況,並將其視爲僅僅是規範不完全清楚的領域。您可能希望向相應的編譯器發送錯誤報告,他們可能希望向WG21發送問題報告以澄清此問題(儘管我不確定我會否認這是規範性規範問題 - 標準在其他地方說,不可推論的背景不參與扣除)。

注爲A1是封閉類模板的參數(並因此成爲正常參數在類被實例化),你可以忽略它們,並保持儘可能模板argment扣而言相同的含義

class Test { 
    public: 
    template <class... A2> void print (A2..., float, double) { 

    } 
}; 

int main() { 
    Test test; 
    test.print<int> (1, 1.0f, 1.0); // works 
} 
+0

您在示例中刪除了函數指針。它仍然失敗,如果我不刪除它:http://ideone.com/I3KY8d – eyelash

+0

@eyelash這就是我說我的答案。 –

+0

@ JohannesSchaub-litb「不可否認性」是否源自兩個參數包(pp)彼此靠近,pp不是函數的最後一個參數或任何其他一般規則集? –