2014-01-25 32 views
3

我遇到了一些非常奇怪的g ++行爲,並希望得到一些幫助。 所以我想在座三人幾乎類似的代碼片段:C++ 11 g ++奇怪(使用默認模板,lambda和remove_if)

class Foo 
{ 
    public: 

    template<typename T = char> 
    bool Moo(std::function<bool()> f = [](){ return true; }) const 
    { 
     std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; 
     str2.erase(std::remove_if(str2.begin(), str2.end(), 
         [](char x){return true;}), str2.end()); 
     // just to do something 
     f = 0; 
     return f(); 
    }; 
}; 

int main(int argc, char **args) 
{ 
    Foo* myFoo = new Foo(); 
    return myFoo->Moo<>(); 
} 

這將產生3個錯誤:對於類封閉「富::武(標準模板參數

  • 默認參數::功能)const的:: __ lambda1' [](炭X){返回true;})
  • 爲調用沒有匹配的函數 '富::武()'
  • 模板參數推導/置換失敗

現在,如果我們改變哞的參數,以一個簡單的類型如果我們拿出函數體內的拉姆達(通過取出整條生產線以「str.erase」)的代碼編譯沒有錯誤!

更改參數瑣碎類型:

class Foo 
{ 
    public: 

    template<typename T = char> 
    bool Moo(bool f = true) const 
    { 
     std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; 
     str2.erase(std::remove_if(str2.begin(), str2.end(), 
         [](char x){return true;}), str2.end()); 
     // just to do something 
     f = 0; 
     return f; 
    }; 
}; 

int main(int argc, char **args) 
{ 
    Foo* myFoo = new Foo(); 
    return myFoo->Moo<>(); 
} 

刪除與 「str.erase」 行:

class Foo 
{ 
    public: 

    template<typename T = char> 
    bool Moo(std::function<bool()> f = [](){ return true; }) const 
    { 
     std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; 
     // just to do something 
     f = 0; 
     return f(); 
    }; 
}; 

int main(int argc, char **args) 
{ 
    Foo* myFoo = new Foo(); 
    return myFoo->Moo<>(); 
} 

那麼,什麼是怎麼回事?爲什麼函數體中的「remove_if-lambda」(「str.erase」行)和函數參數列表中的「默認-Lambda函數」的組合會產生關於函數頭中的「默認模板參數」的錯誤?

+0

? IIRC存在與lambda相關的缺陷作爲默認參數.. – dyp

+0

在[clang ++ 3.5 trunk 198621](http://coliru.stacked-crooked.com/a/d71fa6bd5f1f7659)上編譯得很好(並且工作正常) – dyp

+0

我一直在使用g ++ 4.8.1,但是現在我看到了4.8.2,所以我也會嘗試一下,讓你知道。 –

回答

0

因此,我可以關閉該問題並確認問題:

上述(第一)實施例的代碼上不克++編譯 - 4.8.1,克++ - 4.8.2也不克++ - 4.9。 它確實在clang ++ 3.2-7ubuntu1上編譯。因此,我因爲沒有看到任何語法錯誤而將這稱爲gcc中的錯誤。

我已報告的錯誤的錯誤爲59944:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59944

編輯: 這個錯誤自那時起,已修復++您正在使用哪個克版本的GCC 4.9.2