![](/img/trans.png)
[英]Why can't the compiler deduce template argument for this simple function?
[英]why compiler can't deduce template argument in my code?
我正在使用視覺工作室,我已經嘗試了所有我能想到的東西。 但不知道為什么這段代碼會產生錯誤,這是我的代碼:
template <class A,class B> B returnArgtype(void (A::*)(B)) {return *new B;}
struct test
{
void function(int);
decltype(returnArgtype(&test::function)) x;
};
它會產生這個錯誤:
error C2784: 'A returnArgtype(void (__thiscall A::* )(B))' : could not deduce template argument for 'void (__thiscall A::* )(B)' from 'void (int)'
我想知道當參數 x 在 function 中初始化時它不會產生該錯誤,如下所示:
struct test
{
void function(int)
{
decltype(returnArgtype(&test::function)) x;
}
};
這對我有用(GCC 4.6, -std=c++0x
):
template <class A, class B> B returnArgtype(void (A::*)(B));
struct test
{
void function(int);
decltype(returnArgtype(&test::function)) x;
};
這是我在您的其他問題上鏈接到的相同錯誤(請對其進行投票以使其更有可能 MS 會花時間修復它):
C++ 編譯器在模板推導過程中失去成員函數指針的成員性,導致 ICE
然后,看看@Ise Wistera 的答案,它更簡單,可能不會導致這個問題。
微軟更新了錯誤報告,表示他們已經找到了解決辦法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.