C++ C++;11 g++;陌生度(使用默认模板、lambda和remove_if)

C++ C++;11 g++;陌生度(使用默认模板、lambda和remove_if),c++,templates,c++11,lambda,C++,Templates,C++11,Lambda,我遇到了一些非常奇怪的g++行为,希望能得到一些帮助。 因此,我想介绍三段几乎相似的代码: 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

我遇到了一些非常奇怪的g++行为,希望能得到一些帮助。 因此,我想介绍三段几乎相似的代码:

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<>();
}
class-Foo
{
公众:
模板
boolmoo(std::函数f=[](){return true;})常量
{
std::string str2=“文本\n带\t某些空白\n\n”;
str2.erase(std::remove_if(str2.begin()、str2.end()、str,
[](char x){return true;}),str2.end();
//只是为了做点什么
f=0;
返回f();
};
};
int main(int argc,char**args)
{
Foo*myFoo=newfoo();
返回myFoo->Moo();
}
这将产生3个错误:

  • 包含“Foo::Moo(std::function)const::\uu lambda1”[](char x){return true;})的类的模板参数的默认参数
  • 调用“Foo::Moo()”时没有匹配的函数
  • 模板参数推断/替换失败
现在,如果我们将Moo的参数更改为一个普通类型,或者如果我们在函数体中取出lambda(通过使用“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<>();
}
class-Foo
{
公众:
模板
bool Moo(bool f=true)常数
{
std::string str2=“文本\n带\t某些空白\n\n”;
str2.erase(std::remove_if(str2.begin()、str2.end()、str,
[](char x){return true;}),str2.end();
//只是为了做点什么
f=0;
返回f;
};
};
int main(int argc,char**args)
{
Foo*myFoo=newfoo();
返回myFoo->Moo();
}
删除带有“str.erase”的行:

class-Foo
{
公众:
模板
boolmoo(std::函数f=[](){return true;})常量
{
std::string str2=“文本\n带\t某些空白\n\n”;
//只是为了做点什么
f=0;
返回f();
};
};
int main(int argc,char**args)
{
Foo*myFoo=newfoo();
返回myFoo->Moo();
}

这是怎么回事?为什么函数体(“str.erase”行)中的“remove_if-lambda”和函数参数列表中的“defaulted lambda function”组合在函数标题中的“defaulted template parameter”会产生错误?

因此我可以关闭问题并确认问题:

上面的(第一个)代码示例不在g++-4.8.1、g++-4.8.2和g++-4.9上编译。 它在clang++3.2-7ubuntu1上编译。因此,由于没有看到任何语法错误,我将其称为gcc中的错误

我已将该错误报告为错误59944:

编辑:
从那时起,gcc 4.9.2中已经修复了此错误

您使用的是哪个版本的g++?IIRC有一些与lambdas相关的bug作为默认参数..编译很好(并且“按预期”工作)在上,我一直在使用g++4.8.1,但我看到现在有了4.8.2,所以我也要尝试一下,让你知道。@BM在g++4.9中也没有被修复。我没有用clang尝试过,所以如果g++4.9仍然失败,那么我想我们这里有一个官方错误
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<>();
}