Why are there differences of compiled c++17 lambdas between GCC and clang?

Viewed 250

This reproduces on GCC 7.1 and clang 4.0., also on both snapshots on July 30, 2017.

Example 1: Calling lambdas other than in return statement, both compilers behave differently.

int main()
{
    auto sum = [](auto ... a) { return (... + a); };

    auto value1{sum(5,5)};   // Having optimization disabled -O0,
                             // GCC treats this line as if constexpr is there,
                             // clang doesn't.

    constexpr auto value2{sum(1,2,3,4)};  // Both treat this line as constexpr like expected.

                            // As a result, in GCC, no trace of lambdas in the executable,
                            // and only one lambda in clang.

    return value1 + value2;
}

Live @godbolt

Example 2 and 3: Calling lambdas in return statement, both compilers behave the same way: runtime evaluation/no constexpr.

int main()
{
    auto sum = [](auto ... a) { return (... + a); };

    return sum(5,5) + sum(1,2,3,4);   // Both compilers generate 2 lambdas like expected.

}

Live @godbolt

int main()
{
    constexpr auto sum = [](auto ... a) { return (... + a); };

    return sum(5,5) + sum(1,2,3,4);   // Both compilers generate 2 lambdas like expected.

}

Live @godbolt


Does the standard body let this happen between compilers? Why?

1 Answers
Related