lundi 9 octobre 2017

Wrong gcc behaviour with lambda

By compiling this:

#include <iostream>
#include <sstream>

std::string makeList (std::string sep)
{
    auto makeItem = [&] (std::string item)
    {
        static char count = '0';
        return (++count, count) + sep + item + '\n';
    };

    return makeItem ("first") + makeItem ("second") + makeItem ("third");
}

int main() 
{
    std::cout << makeList (". ");
}

with gcc (5.4.0, c++11 flag) the output is this:

3. first
2. second
1. third

while the correct output, which clang (3.8, c++11 flag) gets, is:

1. first
2. second
3. third

Is there a particular reason for this behaviour?

Aucun commentaire:

Enregistrer un commentaire